check in the collection,what id has no records in mongodb -
i have few ids in array
var editions= ["aaa","bbb","ccc","ddd",......,"zzz"] i have collection called participants
it has record called edition
edition:"aaa" now want ids in editions array has no records in participants collection
if want of happen on server can use aggregation framework , set features available, notably $setdifference operator mongodb 2.6 , greater:
db.collection.aggregate([ { "$group": { "_id": null, "editionsfound": { "$addtoset": "$edition" } }}, { "$project": { "notfound": { "$setdifference": [ editions, "$editionsfound" ] } }} ]) or can on client performing difference between result , original via .distinct() method:
array.prototype.diff = function(a) { return this.filter(function(i) {return a.indexof(i) < 0;}); }; var result = db.collection.distinct("edition"); editions.diff(result); or similar approach in language of choice.
either way means getting distinct values , comparing source list find difference.
Comments
Post a Comment