geoNear例外:'near'字段必须是点

时间:2016-05-11 15:44:33

标签: mongodb mongoose

我在mongo shell中成功运行了以下查询:

db.runCommand({
    geoNear : "stores",
    near : { type : "Point", coordinates : [ -3.978, 50.777 ] },
    spherical : true,
    limit : 10
})

我正在尝试将其转换为我的节点服务as shown in the docs的mongoose查询。

Store.geoNear(
    { type : "Point", coordinates : [-3.978, 50.777]}, 
    { spherical : true, limit : 10 }, 
    function (error, results, stats) {
    //do stuff with results here
});

这会返回以下错误:

MongoError: exception: 'near' field must be point

我的Store架构定义如下:

var storeSchema = new Schema({
    // irrelevant fields...
    locations : [
        {
            address : {
                streetAddress : String,
                postalCode : String
            }
            coords : {
                type : [Number],
                index : '2dsphere'
            }
        }
    ]
})

集合中只有一个2dsphere索引,正如我所提到的,它可以通过shell运行,而不是使用mongoose。

编辑:我尝试过的其他事情没有成功:

回到使用mongodb本机驱动程序(同样的错误,所以我怀疑它是mongodb而不是mongoose导致问题):

Store.collection.geoNear(
    { type : "Point", coordinates : [-3.978, 50.777]}, 
    { spherical : true, limit : 10 }, 
    function (error, results, stats) {
    //do stuff with results here
});

回归使用遗留坐标而不是地理位置(猫鼬和本地(相同的错误)):

Store.geoNear(
    -3.978, 50.777, 
    { spherical : true, limit : 10 }, 
    function (error, results, stats) {
    //do stuff with results here
});

使用来自mongoose的runCommand(这个错误不同,说Store.db.command不是函数 - 从mongoose调用它的正确方法是可接受的答案):

Store.db.command({
    geoNear : "stores",
    near : { type : "Point", coordinates : [ -3.978, 50.777 ] },
    spherical : true,
    limit : 10
}, function(error, results){})

1 个答案:

答案 0 :(得分:1)

最后设法通过mongodb本机驱动程序找出如何回退使用runCommand

Store.db.db.command({
    geoNear : "stores",
    near : { type : "Point", coordinates : [ -3.978, 50.777 ] },
    spherical : true,
    limit : 10
}, function(error, results){})
相关问题