Mongo / Mongoose – 通过部分文档查找

我有一组属性,都有地址。

"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 } } 

我有一个较早的查询返回一个地址数组,如:

 [ { name: 'Massachusetts - Salem', city: 'Salem', _id: 53784206cd73fbae193b62d5, state: [Object] }, { name: 'Illinois - Springfield', city: 'Springfield', _id: 5376fa92bde0e0ea047e9abd, state: [Object] } ] 

我想使用上面的数组的输出来search地址的属性的集合。

例如,我想寻找地址包含“斯普林菲尔德和伊利诺伊州”,“萨利姆城市和马萨诸塞州”的所有房产。

我最初试图做到这一点使用$ in与一些$和混合,但我不知道是否有可能。

您可以尝试在包含从另一个input数组派生的查询条件的数组上使用$or运算符。 例如,使用示例属性集合:

 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 } } } ]) 

以下查询将给出所需的结果:

 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 }); 

示例输出

 /* 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 } } }