For MongoDB 3.6 and above, use the $expr operator, which allows you to use aggregation expressions inside the query language:
var followers_count = 30; db.locations.find({ "$expr": { "$and": [ { "$eq": ["$name", "development"] }, { "$gte": [{ "$size": "$followers" }, followers_count ]} ] } });
For incompatible versions, you can use both $match and $redact to request your collection. For example, if you want to query the locations collection, where the name "development" and followers_count greater than 30, run the following aggregate operation:
const followers_count = 30; Locations.aggregate([ { "$match": { "name": "development" } }, { "$redact": { "$cond": [ { "$gte": [ { "$size": "$followers" }, followers_count ] }, "$$KEEP", "$$PRUNE" ] } } ]).exec((err, locations) => { if (err) throw err; console.log(locations); })
or inside one conveyor as
Locations.aggregate([ { "$redact": { "$cond": [ { "$and": [ { "$eq": ["$name", "development"] }, { "$gte": [ { "$size": "$followers" }, followers_count ] } ] }, "$$KEEP", "$$PRUNE" ] } } ]).exec((err, locations) => { if (err) throw err; console.log(locations); })
The above will return locations using only _id links from users. To return user documents as a means of βpopulatingβ an array of followers, you can add a $lookup pipeline.
If the base version of the Mongo server is 3.4 or newer, you can run the pipeline as
let followers_count = 30; Locations.aggregate([ { "$match": { "name": "development" } }, { "$redact": { "$cond": [ { "$gte": [ { "$size": "$followers" }, followers_count ] }, "$$KEEP", "$$PRUNE" ] } }, { "$lookup": { "from": "users", "localField": "followers", "foreignField": "_id", "as": "followers" } } ]).exec((err, locations) => { if (err) throw err; console.log(locations); })
else you will need a $unwind array of followers before applying $lookup , and then regroup using $group after that:
let followers_count = 30; Locations.aggregate([ { "$match": { "name": "development" } }, { "$redact": { "$cond": [ { "$gte": [ { "$size": "$followers" }, followers_count ] }, "$$KEEP", "$$PRUNE" ] } }, { "$unwind": "$followers" }, { "$lookup": { "from": "users", "localField": "followers", "foreignField": "_id", "as": "follower" } }, { "$unwind": "$follower" }, { "$group": { "_id": "$_id", "created": { "$first": "$created" }, "name": { "$first": "$name" }, "followers": { "$push": "$follower" } } } ]).exec((err, locations) => { if (err) throw err; console.log(locations); })