多个2d索引,不确定运行geoNear的是哪个

Nik*_*yev 4 javascript mongoose mongodb node.js

为什么聚合

    aggregate.near({
      near: coord,
      distanceField: "distance",
      maxDistance: max
    });
Run Code Online (Sandbox Code Playgroud)

可以回来

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

在该计划中唯一的2d指数:

location: { type: [ Number ], index: '2d', sparse: true },
Run Code Online (Sandbox Code Playgroud)

或多或少当我从方案中删除所有错误时,错误会改变原因吗?为什么......

Rik*_*kus 8

当您删除Mongoose模型中的索引(甚至整个字段)时,它不会自动从MongoDB中删除.

运行db.<your collection name>.getIndexes()以查看所有索引.记下name要删除的索引.

运行db.<your collection name>.dropIndex(<name of the index>)以删除索引.

默认情况下,Mongoose告诉MongoDB ensureIndex()在您启动应用程序时(重新)创建索引(" ").