我有一组所有都有地址的属性。
"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]
}
]
我想使用上面数组的输出来按地址搜索属性集合。
例如,我想搜索地址中包含“斯普林菲尔德市和伊利诺伊州”以及“塞勒姆市和马萨诸塞州”的所有房产。
我最初尝试使用 $ in 并将 $和 混合在一起,但是我不确定是否有可能。
答案 0 :(得分:1)
您可以尝试在包含从其他输入数组派生的查询条件的数组上使用 $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
}
}
}