2

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

Tom3652
  • 2,540
  • 3
  • 19
  • 45
  • Would be great if you can share the complete query. Or create a demo in MongoPlayground. Thanks. – Yong Shun May 25 '23 at 09:12
  • I am actually asking for the query as an answer, i don't know how how to write it unfortunately. I have simply pasted the output to help you understand what i expect – Tom3652 May 25 '23 at 09:29
  • 1
    https://mongoplayground.net/p/wHFrV6fHxwU something like this? – cmgchess May 25 '23 at 11:55
  • this is almost perfect, i can work with that ! Is it possible to have a condition that doesn't always select "uid1" but also "uid3" ? – Tom3652 May 25 '23 at 12:08
  • $last for second output instead of $first – cmgchess May 25 '23 at 12:09
  • Sorry, i meant a random between both ? when i run the playground 10 times, it would be nice to have a random 4-5 times uid1 and 5-6 times uid3 for example in my real query i will have thousands of records with `userID1` so i don't want it retrieve always the $first in order – Tom3652 May 25 '23 at 12:10
  • 1
    i dont know if it is possible to do this in the query itself maybe in python you can do something like `"randomDoc": {random.choice(["$first", "$last"]): "$$ROOT"}` – cmgchess May 25 '23 at 16:23
  • i think you will have to go with the above. not possible to randomly run different conditions in the mongo query itself – cmgchess May 26 '23 at 02:59
  • 2
    Here's a [mongoplayground.net example](https://mongoplayground.net/p/8ZvCZekVVpu) that randomly selects the document after `"$group"`. – rickhg12hs May 26 '23 at 03:21
  • 1
    @rickhg12hs nice – cmgchess May 26 '23 at 14:06
  • Sorry i thought i had answered, it's exactly what i needed thank you ! – Tom3652 May 27 '23 at 10:19

0 Answers0