node.js - Mongo/Mongoose - Find by partial document -


i have collection of properties have addresses.

"address" : {     "street" : "5 orange drive",     "city" : "orlando",     "state" : {         "abbreviation" : "fl",         "name" : "florida"     },     "zip" : "32822",     "geo" : {         "lat" : 28.519,         "lng" : -81.304     } }, "address" : {     "street" : "16 main street",     "city" : "tallahassee",     "state" : {         "abbreviation" : "fl",         "name" : "florida"     },     "zip" : "32823",     "geo" : {         "lat" : 28.529,         "lng" : -81.314     } }, "address" : {     "street" : "125 oak drive",     "city" : "salem",     "state" : {         "abbreviation" : "ma",         "name" : "massachusetts"     },     "zip" : "02108",     "geo" : {         "lat" : 24.519,         "lng" : -83.304     } }, "address" : {     "street" : "96 jones way",     "city" : "springfield",     "state" : {         "abbreviation" : "ma",         "name" : "massachusetts"     },     "zip" : "01020",     "geo" : {         "lat" : 28.519,         "lng" : -84.304     } }, "address" : {     "street" : "100 sumner ave",     "city" : "springfield",     "state" : {         "abbreviation" : "il",         "name" : "illinois"     },     "zip" : "32822",     "geo" : {         "lat" : 22.519,         "lng" : -71.304     } }, "address" : {     "street" : "40 roger ave",     "city" : "salem",     "state" : {         "abbreviation" : "al",         "name" : "alabama"     },     "zip" : "32822",     "geo" : {         "lat" : 22.519,         "lng" : -71.304     } } 

i have earlier query returns array of addresses such as:

[     {         name: 'massachusetts - salem',         city: 'salem',         _id: 53784206cd73fbae193b62d5,         state: [object]     }, {         name: 'illinois - springfield',         city: 'springfield',         _id: 5376fa92bde0e0ea047e9abd,         state: [object]     } ] 

i use output of above array search collection of properties address.

such as, search properties address contains 'city of springfield , state of illinois' , 'city of salem , state of massachusetts'.

i trying using $in $and's mixed in wasn't sure if possible.

you try using $or operator on array contains query conditions derived other input array. example, sample properties collection:

db.properties.insert([ {     "address" : {         "street" : "5 orange drive",         "city" : "orlando",         "state" : {             "abbreviation" : "fl",             "name" : "florida"         },         "zip" : "32822",         "geo" : {             "lat" : 28.519,             "lng" : -81.304         }     } }, {     "address" : {         "street" : "16 main street",         "city" : "tallahassee",         "state" : {             "abbreviation" : "fl",             "name" : "florida"         },         "zip" : "32823",         "geo" : {             "lat" : 28.529,             "lng" : -81.314         }     } }, {     "address" : {         "street" : "125 oak drive",         "city" : "salem",         "state" : {             "abbreviation" : "ma",             "name" : "massachusetts"         },         "zip" : "02108",         "geo" : {             "lat" : 24.519,             "lng" : -83.304         }     } }, {     "address" : {         "street" : "96 jones way",         "city" : "springfield",         "state" : {             "abbreviation" : "ma",             "name" : "massachusetts"         },         "zip" : "01020",         "geo" : {             "lat" : 28.519,             "lng" : -84.304         }     } }, {     "address" : {         "street" : "100 sumner ave",         "city" : "springfield",         "state" : {             "abbreviation" : "il",             "name" : "illinois"         },         "zip" : "32822",         "geo" : {             "lat" : 22.519,             "lng" : -71.304         }     } }, {     "address" : {         "street" : "40 roger ave",         "city" : "salem",         "state" : {             "abbreviation" : "al",             "name" : "alabama"         },         "zip" : "32822",         "geo" : {             "lat" : 22.519,             "lng" : -71.304         }     } } ]) 

the following query give desired result:

var res = [     {         "name": 'massachusetts - salem',         "city": 'salem',         "_id": "53784206cd73fbae193b62d5",         "state": [{"name": "massachusetts", "abbreviation": "ma"}]     }, {         "name": 'illinois - springfield',         "city": 'springfield',         "_id": "5376fa92bde0e0ea047e9abd",         "state": [{"name": "illinois", "abbreviation": "il"}]     } ];  var condition = res.map(function (item){     return {         "address.city": item.city,         "address.state.name": item.state[0].name     } });  db.properties.find({"$or": condition }); 

sample output

/* 0 */ {     "_id" : objectid("557848b43cab061ff5c618b7"),     "address" : {         "street" : "125 oak drive",         "city" : "salem",         "state" : {             "abbreviation" : "ma",             "name" : "massachusetts"         },         "zip" : "02108",         "geo" : {             "lat" : 24.519,             "lng" : -83.304         }     } }  /* 1 */ {     "_id" : objectid("557848b43cab061ff5c618b9"),     "address" : {         "street" : "100 sumner ave",         "city" : "springfield",         "state" : {             "abbreviation" : "il",             "name" : "illinois"         },         "zip" : "32822",         "geo" : {             "lat" : 22.519,             "lng" : -71.304         }     } } 

Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -