MongoDB $ near查询不返回任何结果

时间:2014-09-24 06:44:29

标签: mongodb indexing mongodb-query geospatial

我使用find()打印出一些记录,然后基于其中一个记录构建一个$ near查询。查询不返回任何结果。这很简单,但却完全错了。这里发生了什么?

> db.locations.find()
{ "_id" : ObjectId("542264d7050ff67199103c94"), "loc" : "13", "fbid" : "13", "lnglat" : { "geo" : [ -122.58456, 37.918721000000005 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103c8f"), "loc" : "8", "fbid" : "8", "lnglat" : { "geo" : [ -122.53456, 37.868721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103c8a"), "loc" : "3", "fbid" : "3", "lnglat" : { "geo" : [ -122.48456, 37.818721000000004 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103cad"), "loc" : "38", "fbid" : "38", "lnglat" : { "geo" : [ -122.83456, 38.168721000000005 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103c99"), "loc" : "18", "fbid" : "18", "lnglat" : { "geo" : [ -122.63456000000001, 37.968721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103ca8"), "loc" : "33", "fbid" : "33", "lnglat" : { "geo" : [ -122.78456, 38.118721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103ca3"), "loc" : "28", "fbid" : "28", "lnglat" : { "geo" : [ -122.73456, 38.068721000000004 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103cb2"), "loc" : "43", "fbid" : "43", "lnglat" : { "geo" : [ -122.88456000000001, 38.218721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103c9e"), "loc" : "23", "fbid" : "23", "lnglat" : { "geo" : [ -122.68456, 38.018721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103cb7"), "loc" : "48", "fbid" : "48", "lnglat" : { "geo" : [ -122.93456, 38.268721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103c90"), "loc" : "9", "fbid" : "9", "lnglat" : { "geo" : [ -122.54456, 37.878721000000006 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103c8b"), "loc" : "4", "fbid" : "4", "lnglat" : { "geo" : [ -122.49456, 37.828721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103c95"), "loc" : "14", "fbid" : "14", "lnglat" : { "geo" : [ -122.59456, 37.928721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103c9a"), "loc" : "19", "fbid" : "19", "lnglat" : { "geo" : [ -122.64456, 37.978721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103c9f"), "loc" : "24", "fbid" : "24", "lnglat" : { "geo" : [ -122.69456, 38.028721000000004 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103ca4"), "loc" : "29", "fbid" : "29", "lnglat" : { "geo" : [ -122.74456, 38.078721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103ca9"), "loc" : "34", "fbid" : "34", "lnglat" : { "geo" : [ -122.79456, 38.128721000000006 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103cae"), "loc" : "39", "fbid" : "39", "lnglat" : { "geo" : [ -122.84456, 38.178721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103cb3"), "loc" : "44", "fbid" : "44", "lnglat" : { "geo" : [ -122.89456, 38.228721 ] }, "__v" : 0 }
{ "_id" : ObjectId("542264d7050ff67199103cb8"), "loc" : "49", "fbid" : "49", "lnglat" : { "geo" : [ -122.94456, 38.278721000000004 ] }, "__v" : 0 }
Type "it" for more
> db.locations.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "mydb.locations"
        },
        {
                "v" : 1,
                "key" : {
                        "lnglat.geo" : "2dsphere"
                },
                "name" : "lnglat.geo_2dsphere",
                "ns" : "mydb.locations",
                "background" : true,
                "safe" : null,
                "2dsphereIndexVersion" : 2
        },
        {
                "v" : 1,
                "unique" : true,
                "key" : {
                        "loc" : 1
                },
                "name" : "loc_1",
                "ns" : "mydb.locations",
                "background" : true,
                "safe" : null
        }
]
> db.locations.count( { lnglat: { geo: { $near: { $geometry: { type: "Point", coordinates: [ 122.83456, 38.16872100 ] }, maxDistance: 5000 } } } } )
0

当我修改lnglat: { geo: {中的字段时,Mongo抱怨它无法找到运行查询的索引。

1 个答案:

答案 0 :(得分:1)

您的“签名”在您的坐标上是错误的,并且您“应该”在此处应用点符号,以防万一您将来更改此子文档:

db.locations.count({ 
    "lnglat.geo": { 
        "$near": { 
            "$geometry": { 
                "type": "Point", 
                "coordinates": [ -122.83456, 38.16872100 ],
            },
            "$maxDistance": 5000
        }
    }
});

$maxDistance未正确指定,因为它需要前面的$

在此处包含的示例文档中,距离内的计数为2