java mongodb near,mongodb – geoNear例外:’near’字段必须是点

我在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

我的商店架构定义如下:

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

});

回归使用遗留坐标而不是地理点(mongoose和native(相同的错误)):

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){})

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

Store.db.db.command({

geoNear : "stores",

near : { type : "Point", coordinates : [ -3.978, 50.777 ] },

spherical : true,

limit : 10

}, function(error, results){})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值