mongodb - How to check if a portion of an _id from one collection appears in another -
i have collection _id of form [message_code]-[language_code]
, _id [message_code]
. i'd find documents first collection message_code portion of _id not appear in second collection.
example:
> db.cola.find({}) { "_id" : "trm1-en" } { "_id" : "trm1-es" } { "_id" : "trm2-en" } { "_id" : "trm2-es" } > db.colb.find({}) { "_id" : "trm1" }
i want query return trm2-en , trm-es cola. of course in live data, there thousands of records in each collection.
according this question trying similar, have save results query against colb , use in $in condition in query against cola. in case, need strip -[language_code]
portion before doing comparison, can't find way so.
if else fails, i'll create new field in cola contains message code, there better way it?
edit: based on michael's answer, able come solution:
var arr = db.colb.distinct("_id") var regexs = arr.map(function(elm){ return new regexp(elm); }) var result = db.cola.find({_id : {$nin : regexs}}, {_id : true})
edit: upon closer inspection, above method doesn't work after all. in end, had add new field.
disclaimer: little hack may not end well.
- get distinct
_id
usingcollection.distinct
method. - build regular expression array using
array.prototype.map()
var arr = db.colb.distinct('_id'); arr.map(function(elm, inx, tab) { tab[inx] = new regexp(elm); }); db.cola.find({ '_id': { '$nin': arr }})
Comments
Post a Comment