mardi 21 juillet 2020

get random documents with units property on mongo

Having a cards collection like this:

[
 { _id: ObjectId("5f15fb73a3ab488e17abea08"), units: 4 }, 
 { _id: ObjectId("2r346b61r56g65t532sr45h1"), units: 4 },
] 

I would like use aggregate or similar (without modify documents) for get 3 random cards, with possibility to ignore in use cards array like [ObjectId("2r346b61r56g65t532sr45h1"), ObjectId("2r346b61r56g65t532sr45h1"), ObjectId("5f15fb73a3ab488e17abea08")].

db.cards.aggregate([
    {$match: { '_id': {'$nin': []'}}},
    { $sample: { size: 3 } }
])

It is possible?




Aucun commentaire:

Enregistrer un commentaire