javascript - 异常:geoNear 命令失败:errmsg:多个二维索引
问题描述
我正在尝试按距离从集合中检索文档。我尝试使用$geoNear
聚合,但要么遇到错误(使用 node.js 和邮递员),要么返回 0 条记录。
样本文件:
{
"_id" : ObjectId("5cc37692fe9fd54b3cd136c9"),
"category" : "art",
"description" : "an eastbound description for The soda event.",
"geometry" : {
"type" : "Point",
"coordinates" : [
3.60178480057443,
6.46123057784917
]
}
"__v" : 0
}
.
.
模型:
const GeoSchema = new Schema({
type: {
type: String,
default: "Point"
},
coordinates: {
type: [Number],
index: "2dsphere"
}
}, { _id: false });
const EventSchema = new Schema({
category: String,
description: String,
geometry: GeoSchema,
});
询问
db.events.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [3.60178480057443, 6.46123057784917] },
distanceField: "dist",
maxDistance: 90000,
spherical: true
}
}
]);
运行上面的查询返回零个结果,一个空数组(在邮递员上),有时会显示这个错误:
{
"name": "MongoError",
"errmsg": "exception: geoNear command failed: { ok: 0.0, errmsg: \"more than one 2d index, not sure which to run geoNear on\" }",
"code": 16604,
"ok": 0
}
当显示此错误时,我运行该db.events.dropIndex
函数。我复制并粘贴了文档中使用的示例,效果很好。请问如何使这个 $geoNear 函数工作。我已经为此苦苦挣扎了一整天。
解决方案
推荐阅读
- java - 将多个对象插入到arraylist
- google-apps-script - 触发日期倒计时时发送电子邮件
- mysql - 如何显示最后 4 位数字并将其余数字替换为 XXXXXXXXX
- android - 杀死服务
- fullcalendar - FullCalendar 营业时间和时区
- c# - Visual Studio 2017 发布签名工具报错
- ios - What is the Max Image Size (File Size or Pixel Size) I can Upload to a Website with iOS Safari?
- opengl - 纹理图集中的现代 OpenGL 平铺区域(正确在着色器中)
- excel - Excel VBA 非英文字符支持读取文件
- javascript - 使用键和值将数组转换为 JSON