mongodb - How to get exact document result from key value type of embedded documents -


let have kind of document structured, attributes field embedded document , i've indexed attributes.key , attributes.value

1------------------------------------------------------------------------------------- {    "_id" : objectid( "5191d8e5d00560402e000001" ),   "attributes" : [    { "key" : "pobox","value" : "qakuwo" },    { "key" : "city", "value" : "cbdrip" },    { "key" : "address","value" : "zmycaa" } ],   "email" : "fwaudl_2@email.com",   "firstname" : "fwaudl_2"  } 2------------------------------------------------------------------------------------- {    "_id" : objectid( "5191d8e7d00560402e000055" ),   "attributes" : [      { "key" : "pobox", "value" : "snfriy" },      { "key" : "city", "value" : "jpdvri" },      { "key" : "address", "value" : "pholuw" } ],   "email" : "hqynwh_86@email.com",   "firstname" : "hqynwh_86"  } 

my problem how exact document when querying based on attributes field,

db.app.find({ attributes.key:address , attributes.value:/.*uw.*/i }) 

the query result not expected, should result 2nd document without 1st document. know put regex on attributes.value, expecting check attributes.key have address value.

and if want filter key, such like,

db.app.find({ attributes.key:address , attributes.value:/.*uw.*/i , attributes.key:city , attributes.value:/.*ri.*/i }) 

any opinion helpful guys. thx.

just investigated little , figured out following. following uses index mentioned below. can explain() on find() check more index usage details

db.testing.getindexkeys() [ { "_id" : 1 }, { "attributes.key" : 1, "attributes.value" : 1 } ]  test:mongo > db.testing.find({$and : [ { attributes : {$elemmatch : {key : 'address', value : /.*uw.*/i }} }, { attributes : {$elemmatch : {key : 'city', value : /.*ri.*/i }} }] }).pretty() {     "_id" : objectid("5191d8e7d00560402e000055"),     "attributes" : [         {             "key" : "pobox",             "value" : "snfriy"         },         {             "key" : "city",             "value" : "jpdvri"         },         {             "key" : "address",             "value" : "pholuw"         }     ],     "email" : "hqynwh_86@email.com",     "firstname" : "hqynwh_86" } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -