你终于可以看到我的Mongodb记录了。。。我现在正在尝试实现搜索功能,我为我的项目疯狂地进行复选框过滤,下面我在点击多个复选框后列出了这些数组(参见1、2和3(。
我尝试使用$in聚合多个匹配查询,但没有成功。下面的数组用于检查记录。
例如:["餐厅"、"商场"]需要向;commercialType";在记录中,同时["AC室"、"三相电"]需要与;propertyFeatures.name";在记录中。。因此,如果存在带有这些筛选的记录,则必须显示所有匹配的记录。
I tried with multiple $in queries like this, but it gives empty records.
"$match": {
"commercialType": {
"$in": ["Restaurant", "Hotel"]
},
{
"propertyFeatures.name": {
"$in": ['AC Rooms']
}
},
... other match filters
}
1. Below Array is used to find commercialType (field in doc)
[
'Restaurant',
'Office space',
'Hotel'
]
2. Below Array is used to find landType (field in doc)
[
'Bare land',
'Beachfront land',
'Coconut land'
]
3. Below Array is used to find "propertyFeatures.name" (field in doc)
[
'AC Rooms',
'3 Phase Electricity',
'Hot Water'
]
[
{
"_id": {
"$oid": "6343b68edf5e889a575c8502"
},
"propertyType": "House",
"propertyFeatures": [
{
"id": 1,
"name": "AC Rooms",
"value": true
}
]
},
{
"_id": {
"$oid": "6343b68edf5e889a575c8502"
},
"propertyType": "Land",
"landType": "Bare land",
"propertyFeatures": [
{
"id": 1,
"name": "Wider Road",
"value": true
}
]
},
{
"_id": {
"$oid": "6343b68edf5e889a575c8502"
},
"propertyType": "Commercial",
"commercialType": "Restaurant",
"propertyFeatures": [
{
"id": 1,
"name": "3 Phase Electricity",
"value": true
}
]
}
]
您可能缺少$或运算符,因此您的示例管道变成
[
{"$match": {
"$or": [
{
"commercialType": {
"$in": ["Restaurant", "Hotel"]
},
{
"propertyFeatures.name": {
"$in": ['AC Rooms']
}
}
]
}
]
MongoDB文档:https://www.mongodb.com/docs/manual/reference/operator/aggregation/or/#error-处理