json - Select document having particular key value pair but not having other key value pair -


i have collection named "collection".
has 2 documents shown below -
document a

 {                 "genericparams" : [                     {                         "key" : "sms_email_count",                         "value" : 3                     },                     {                         "key" : "first_sms_email_time",                         "value" : numberlong("1450691202568")                     },                     {                         "key" : "second_sms_email_time",                         "value" : numberlong("1450691202568")                     },                     {                         "key" : "third_sms_email_time",                         "value" : numberlong("1450691202568")                     },                     {                         "key" : "manual_refund_processed",                         "value" : "false"                     }                 ]             } 

document b

 {                 "genericparams" : [                     {                         "key" : "sms_email_count",                         "value" : 3                     },                     {                         "key" : "first_sms_email_time",                         "value" : numberlong("1450691202568")                     },                     {                         "key" : "second_sms_email_time",                         "value" : numberlong("1450691202568")                     },                     {                         "key" : "third_sms_email_time",                         "value" : numberlong("1450691202568")                     }                 ]             } 

i want make query output document b
logic query want document have key value pair "key" : "third_sms_email_time" not "key" : "manual_refund_processed".
document out here refers document a/b. :)

 {                 "genericparams" : [                     {                         "key" : "sms_email_count",                         "value" : 3                     },                     {                         "key" : "first_sms_email_time",                         "value" : numberlong("1450691202568")                     },                     {                         "key" : "second_sms_email_time",                         "value" : numberlong("1450691202568")                     },                     {                         "key" : "third_sms_email_time",                         "value" : numberlong("1450691202568")                     }                 ]             } 

what have tried -

db.collection.aggregate([     {$match: { "genericparams.key": { $exists: true, $nin: [ "manual_refund_processed" ] }, "currentstate.genericparams.key": "third_sms_email_time"   }},     { $project : {         "genericparams" : 1      }}   ]) 

your query attempt uses same "key" twice. cannot in object structure "overwiting" value of same key. actual query considered "second" condition key.

so if want have multiple conditions same key, use $and operator:

db.collection.aggregate([     { "$match": {         "$and": [             { "genericparams.key": { "$exists": true, "$ne": "manual_funds_processed" } },             { "genericparams.key": "third_sms_email_time" }         ]      },     // other stages }) 

or since mongodb conditions "and" arguments default, can specify $eq in case:

db.collection.aggregate([     { "$match": {         "genericparams.key": {              "$ne": "manual_refund_processed",              "$eq": "third_sms_email_time"         }     }},     // other stages ]) 

noting here there nothing special .aggregate() here itself, base "query" part doing work of document selection.

also note, "positive" condition present ( $eq ) not necessary use $exists since testing @ least element needs match.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -