【问题标题】:MongoDB 2.6 geospatial $near query behavior changed on compound index?MongoDB 2.6 地理空间 $near 查询行为在复合索引上发生了变化?
【发布时间】:2014-06-07 11:22:06
【问题描述】:

我一直在使用复合索引运行地理空间查询。

升级到 MongoDB 2.6 后,结果集的顺序发生了变化,因此以下查询的结果不再按距离排序:

db.properties.find({ 
    "address.uppercase": { "$regex": "^14529" }, 
    "address.location": { 
        "$near": { 
            "$geometry": { 
                "type": "Point", 
                "coordinates": [ -122.2103, 47.6154 ] 
            }
        }
    }
})

属性集合上的索引配置为:

{ "address.uppercase": 1, "address.location": "2dsphere" }

在 2.4 中,查询返回“address.uppercase”以“14529”开头的所有地址,按与位置[ -122.2103, 47.6154 ]的地址距离排序。

升级到 2.6 后,相同的查询现在返回所有按“address.uppercase”排序的地址。

有没有办法根据查询的地理空间部分指定结果的排序顺序?

【问题讨论】:

    标签: mongodb geospatial


    【解决方案1】:

    抱歉,我没有看到这种行为。使用示例文档:

    { 
        "address" : { 
            "uppercase" : "14529", 
            "location" : { 
                "type" : "Point", 
                "coordinates" : [ -100.2014, 47.6154 ] 
            } 
        }
    },
    {
        "address" : {
            "uppercase" : "90210",
            "location" : {
                "type" : "Point",
                "coordinates" : [ -120.2014, 47.6154 ]
            }
        }
    },
    {
        "address" : { 
            "uppercase" : "14529", 
            "location" : { 
                "type" : "Point", 
                "coordinates" : [ -120.2014, 47.6154 ] 
            }
        }
    }
    

    还有查询:

    db.address.find({
        "address.uppercase": { "$regex": "^14529" },
        "address.location": {
            "$near": {
                "$geometry": {
                     "type": "Point",
                     "coordinates": [ -122.2103, 47.6154 ]
                }
            }
        }
    })
    

    我按最近的顺序得到这些:

    { 
        "address" : { 
            "uppercase" : "14529", 
            "location" : { 
                "type" : "Point", 
                "coordinates" : [ -120.2014, 47.6154 ]
            }
        }
    },
    { 
        "address" : { 
            "uppercase" : "14529", 
            "location" : { 
                "type" : "Point", 
                "coordinates" : [ -100.2014, 47.6154 ]
            }
        }
    }
    

    甚至回归索引类型。也许尝试删除索引并重新创建它,看看你是否可以重现。否则,如果您可能有一个不符合这些条件的极端案例示例,那么您的问题可以通过编辑来包含所需的详细信息。

    【讨论】:

    • 感谢您关注这个尼尔。我也已经在 MongoDB 错误跟踪器上报告了这个问题,如果集合中的至少一个文档包含多个“address.uppercase”字段的值(即数组),那么这是一个错误。见:jira.mongodb.org/browse/SERVER-13687
    【解决方案2】:

    已确认该错误发生在复合多键 2dsphere 索引中。 IE。只有当集合中的一个文档对于索引定义的字段之一具有多个值(即数组)时,它才会显示自己。

    10gen 将在 MongoDB 2.6.2 中发布修复。见:https://jira.mongodb.org/browse/SERVER-13687

    【讨论】:

      猜你喜欢
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 2012-11-29
      • 2017-01-25
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 2019-11-30
      相关资源
      最近更新 更多