geoNear例外:“ near”字段必须为point

Car*_*sel 5 mongoose mongodb

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

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

我正在尝试将其转换为对node服务的猫鼬查询, 如docs所示

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

这将返回以下错误:

MongoError: exception: 'near' field must be point
Run Code Online (Sandbox Code Playgroud)

我的Store架构是这样定义的:

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

集合上只有一个2dsphere索引,而且正如我提到的那样,它通过外壳工作,但不适用于猫鼬。

编辑:我尝试不成功的其他事情:

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

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

退回到使用传统坐标而不是地理点(猫鼬和本地坐标(相同错误)):

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

使用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){})
Run Code Online (Sandbox Code Playgroud)

Car*_*sel 1

最终设法解决如何runCommand通过 mongodb 本机驱动程序回退使用:

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