I have the same question as asked here but i don't really understand the solution.
I have tried to aggregate
and use the $group
field in the pipeline but when i have tried it, it only returns the values inside the $group and not the whole documents.
I want to retrieve let's say 20 documents based on several conditions and the result should contain a unique field value.
Here is an example document :
{
"_id": "uid1",
"public": true,
"closed": false,
"userID": "userID1",
"url": "url1"
}
Now let's say i also have :
{
"_id": "uid2",
"public": true,
"closed": false,
"userID": "userID2",
"url": "url2"
},
{
"_id": "uid3",
"public": true,
"closed": false,
"userID": "userID1",
"url": "url3"
},
{
"_id": "uid4",
"public": true,
"closed": true,
"userID": "userID3",
"url": "url4"
}
The query i am looking for should retrieve :
{
"_id": "uid1",
"public": true,
"closed": false,
"userID": "userID1",
"url": "url1"
},
{
"_id": "uid2",
"public": true,
"closed": false,
"userID": "userID2",
"url": "url2"
}
OR
{
"_id": "uid3",
"public": true,
"closed": false,
"userID": "userID1",
"url": "url3"
},
{
"_id": "uid2",
"public": true,
"closed": false,
"userID": "userID2",
"url": "url2"
}
By querying with {"closed": false, "public": true}
, and userID
must be unique in the results.
Thanks in advance