Mongodb $sample after filtering -
let's want person find people they're not connected with, do:
user.find({ _id: { $nin: req.user.connections })
however, want retrieve @ 10 random documents return. in mongodb, there $sample:
{ $sample: { size: <positive integer> } }
i've never used mongo before, i'm unsure of how chain these 2 in order me retrieve 10 random people current user unconnected to.
$sample
aggregation operator, need create aggregate
pipeline chains 2 operations together:
user.aggregate([ { $match: { _id: { $nin: req.user.connections } } }, { $sample: { size: 10 } } ])
Comments
Post a Comment