我有一个名为“location”的集合,其中一个文档看起来像:
{
"_id" : ObjectId("5726ad18bdb861058819ea00"),
"loc" : {
"type" : "Point",
"coordinates" : [
-90.426407,
34.850685
]
},
"name" : "restaurant name",
"tags" : [
"Chinese",
"thai",
"American",
"indian"
]
}
我有以下索引:
db.location.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "chatx.location"
},
{
"v" : 1,
"key" : {
"loc" : "2dsphere",
"tags" : 1
},
"name" : "loc_2dsphere_tags_1",
"ns" : "chatx.location",
"2dsphereIndexVersion" : 3
}
]
我用标签来识别餐馆的食物类型。
我一直在尝试根据食物类型找出最近的餐馆。我的疑问是:
db.runCommand(
{
geoNear: "location",
near: { type: "Point", coordinates: [-90.426407, 34.850685] },
limit:10,
maxDistance:5000,
spherical: true,
query: { tags:{$all:["chinese", "thai"]}}
}
)
为了做到这一点,我使用了$ geoNear(在3.2版本中表现非常好)。然而,当我与标签结合使用(使用$ all)时,查询会变得更慢,这是有道理的,因为$ all会根据第一个元素搜索所有文档,然后根据其他元素进行优化。查询变慢的主要原因是增加限制(例如:10到100或更多)和数组元素[“泰语”,“印度语”,.....]。例如,如果我搜索具有4种食物类型标签的餐馆,则查询变得非常慢,而不是如果我搜索具有1种食物类型标签的餐馆。
我想知道是否有人可以帮我找出这个集合的最佳架构或提供更好的查询选项。