MongoDB query produces OR response instead of AND -
i'm using mongodb version 2.6.5 , have collection looks this:
{ "_id": objectid("555a3398f4c572a44f877dcd"), "created": isodate("2015-05-18t17:02:14.951z"), "values": [ { "value": "4", "label": "apple" }, { "value": "5", "label": "peach" }, { "value": "5", "label": "banana" }, { "value": "4", "label": "orange" } ], "__v": 0 } { "_id": objectid("555a74dbdfe135105faccdf7"), "created": isodate("2015-05-18t21:27:37.064z"), "values": [ { "value": "2", "label": "apple" }, { "value": "3", "label": "peach" }, { "value": "4", "label": "banana" }, { "value": "5", "label": "orange" } ], "__v": 0 } { "_id": objectid("555a74f9dfe135105faccdfa"), "created": isodate("2015-05-18t21:27:37.064z"), "values": [ { "value": "1", "label": "apple" }, { "value": "1", "label": "peach" }, { "value": "1", "label": "banana" }, { "value": "1", "label": "orange" } ], "__v": 0 } when try document has values.label of "orange" , values.value of "5" should 1 document i'm getting two:
> db.answers.count({ 'values.label':'orange', 'values.value':'5' }) > 2 this selecting 2 documents ids: 555a3398f4c572a44f877dcd (presumably because banana has value of 5 ) , 555a74dbdfe135105faccdf7 (which correct one). can think of why happening?
you're using dot notation (used reach objects) on array. while works in way described, imply or-logic because entire array interpreted object, speak.
you're looking match multiple criteria on documents in array, $elemmatch operator for, i.e.
db.answers.count({ 'values' : { '$elemmatch' : { 'label':'orange', 'value':'5' } } })
Comments
Post a Comment