Imagine a scenario where there is a Schema named MyDoc
with a field called key
. Here are the sample documents:
[
{
"key": 1
},
{
"key": 2
},
{
"key": 2
},
{
"key": 3
},
{
"key": 3
},
{
"key": 3
},
{
"key": 4
}
]
Using mongoose, the goal is to retrieve documents that have a unique value in the key
field. In other words, fetch all documents where no other document shares the same key
value.
In the aforementioned example data, there are multiple documents with keys 2
and 3
, while only one document each has keys 1
and 4
. Therefore, the desired outcome would be to return only the documents with keys 1
and 4
.