mongodb find exact document -
im looking way in mongodb find exact document, meaning dont want match document other fields ones expected.
this can on subdocuments
> db.test.insert({a:{b:1,c:2}}); > db.test.insert({a:{b:1}}); > db.test.find({'a.b':1}); // non exact match {a:{b:1,c:2}} {a:{b:1}} > db.test.find({'a':{'b':1}}); // exact match {a:{b:1}}
i'd same on main document (not subdocument). but
> db.test.insert({b:1,c:2}); > db.test.insert({b:1}); > db.test.find({'b':1}); // non exact match {a:{b:1,c:2}} {a:{b:1}} > db.test.find({'.':{'b':1}}); // not work :( > db.test.find({'b':1, 'c':null}); // works, how supposed know 'c' exists ??? {a:{b:1}}
the final goal being $pull
on arrays
> db.test.insert({a:[{b:1,c:2},{b:1,d:3},{b:1,c:2},{b:1,c:2,d:3}]}); > db.test.update({}, {$pull:{'a':{b:1,c:2}}}); > db.test.find(); {a:[{b:1,d:3}]} // noooo :'(
has idea ?
edit :
here precision on exact / partial matching found on arrays :
- array item exact matching :
db.test.find({'a':{b:1,c:2}});
- array item partial matching :
db.test.find({'a':{$elemmatch:{b:1,c:2}}});
- array item removal partial matching :
db.test.update({}, {$pull:{'a':{b:1,c:2}}})
- array item removal exact matching :
db.test.update({}, {$pullall:{'a':[{b:1,c:2}]}})
(thanks johnnyhk)
it's not documented, if use $pullall
instead of $pull
, matching elements removed:
db.test.update({}, {$pullall:{a: [{b:1,c:2}]}});
Comments
Post a Comment