添加$ maxDistance

时间:2016-07-13 09:41:50

标签: mongodb indexing geolocation geospatial database

我有一个结构如下的数据库:

{
    "_id" : ObjectId("5785663bda7b71a0c5601c89"),
    "visible" : true,
    "created" : {
        "changeset" : "29670315",
        "user" : "samit_53",
        "version" : "1",
        "uid" : "2297171",
        "timestamp" : "2015-03-23T03:59:14Z"
    },
    "type" : "node",
    "id" : "3413812873",
    "pos" : [
        22.1173487,
        88.0034742
    ]
}

现在我在pos

上有'2d'索引
>>db.kol_sample.getIndexes()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "project_sample.kol_sample",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "pos" : "2d"
        },
        "ns" : "project_sample.kol_sample",
        "name" : "pos_2d"
    }
]

现在,当我只查询$ near时,我得到100个结果

db.kol_sample.findOne({"pos":{$near:[22,88]}})
{
    "_id" : ObjectId("5785663bda7b71a0c5601c89"),
    "visible" : true,
    "created" : {
        "changeset" : "29670315",
        "user" : "samit_53",
        "version" : "1",
        "uid" : "2297171",
        "timestamp" : "2015-03-23T03:59:14Z"
    },
    "type" : "node",
    "id" : "3413812873",
    "pos" : [
        22.1173487,
        88.0034742
    ]
}

但在我添加$ maxDistance参数后,无论我传递的是什么距离,它总是返回null

db.kol_sample.findOne({"pos":{$near:[22.1173487,88.0034742]},$maxDistance:100})
null

MongoDB shell版本:2.4.9 32位

1 个答案:

答案 0 :(得分:0)

我使用了错误的语法,我需要在传递给pos

的字典中传递$ maxDistance

<强>错误

db.kol_sample.find({"pos":{$near:[22.1173487,88.0034742]},$maxDistance:100})

<强>正确

db.kol_sample.find({pos:{$near:[22.1173487,88.0034742],$maxDistance:1/111.2}})