gpt4 book ai didi

javascript - MongooseJS 几乎无法使用 Keystone Location 字段

转载 作者:行者123 更新时间:2023-11-30 17:26:07 24 4
gpt4 key购买 nike

我有一个地址字段类型为 Location(KeystoneJS 地址 + 地理坐标)的模型。据我了解,该字段中的地理坐标索引为 2dSphere。

但是,如果我尝试使用 MongooseJS near 函数进行查询,它会返回一个错误:

var point = [3.2642048999999815241,50.830799100000090124];
Challenge.model.find(
{status: 1}
)
.where('location')
.near({
center: point,
maxDistance: maxDistance,
spherical: true
})
.populate('waypoints')
.exec(function(err, challenges) {
if(err) return res.apiError('1000', Errors['1000']);
return res.apiResponse({
challenges: challenges
});
});

返回:

MongoError: can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { status: 1, location: { $nearSphere: [ 3.264204899999982, 50.83079910000009 ], $maxDistance: 1000 } }

我不明白为什么它会为此抛出错误。

最佳答案

似乎是 MongooseJS 中的错误。如果您使用 MongoDB 查询符号,它会起作用:

Challenge.model.find(
{
status: 1,
"location.geo": {$near: { $geometry:{ type: "Point", coordinates: location }, $maxDistance: maxDistance}}},
"-__v -updatedOn -createdOn -slug")
.populate('waypoints')
.exec(function(err, challenges) {
if(err) return res.apiError('1000', Errors['1000']);
return res.apiResponse({
challenges: challenges
});
});

关于javascript - MongooseJS 几乎无法使用 Keystone Location 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24243313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com