Probably the best approach with MongoDB 2.6 or more is using .aggregate()with $redact:
db.collection.aggregate([
{ "$redact": {
"$cond": {
"if": {
"$gt": [
{ "$size": {
"$setIntersection": [
{ "$map": {
"input": "$message",
"as": "msg",
"in": "$$msg.message_id"
}},
"$message_id"
]
}},
0
]
},
"then": "$$PRUNE",
"else": "$$KEEP"
}
}}
])
" " , $map , "message_id" "messages" "message_id", $setIntersection. , "", , , "" . , $setIsSubset.
, $size , 0, . true "", "".
$where JavaScript- , :
db.collection.find({
"$where": function() {
var self = this;
return self.message.map(function(el) {
return el.message_id;
}).filter(function(el) {
return self.mesage_id.indexOf(el) != -1
}).length == 0;
}
})
, , , . pre MongoDB 2.6.
, "" . , $where , .