node.js - MongoDB - Query array that contains nothing but specific values (javascript) -
im new mongodb , node. new forum please patient me.
when have documents these:
{_id: '1', name: 'doc1', values: [{name: 'val1', value: 'x'}]} {_id: '2', name: 'doc2', values: [{name: 'val1', value: 'x'}, {name: 'val2', value: 'x'}]} {_id: '3', name: 'doc3', values: [{name: 'val1', value: 'x'}, {name: 'val2', value: 'x'}, {name: 'val3', value: 'x'}]} {_id: '4', name: 'doc4', values: [{name: 'val1', value: 'x'}, {name: 'val3', value: 'x'}]}
how search documents wich contain not (necessarily) given values like:
db.collection('col1').find(val1, val2);
and results:
doc1, doc2
the way, found accomplish following:
db.collection('col1').find({'values.name': {$nin: {['val3', everyotherpossiblevalue,...]}}})
but still search better (and - because everyotherpossiblevalue lot of values - shorter) query same thing
help appreciated :)
try this:
db.collection('col1').aggregate([{$unwind:'$values'},{$match:{$in:['val1','val2']}},{$group:{_id:"$name"}}])
Comments
Post a Comment