【问题标题】:MongoError: can't find any special indices: 2d (needs index), 2dsphere (needs index)MongoError:找不到任何特殊索引:2d(需要索引),2dsphere(需要索引)
【发布时间】:2013-10-13 08:28:11
【问题描述】:

我正在尝试通过使用 MongoDB 的 find 方法查询某个点周围的纬度和经度点来使用 MongoDB 的地理空间索引。我不断收到错误:

MongoError:找不到任何特殊索引:2d(需要索引)、2dsphere(需要索引)

在谷歌搜索大约一个小时后,我不确定文档在哪里。我也找不到任何好的解释。这是我使用 Mongoose 创建的 Schema:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var EventSchema = new Schema ({
  name: String,
  description: String,
  location: {type: [String], index: '2dsphere'},
  time: Date,
  photo: Buffer,
  activity: String
});

mongoose.model('Event', EventSchema);

然后我使用 find 方法在用户提供的点周围查找其他 Event 文档。

var maxDistance = 0.09;
var lonLat = {$geometry: {type: 'Point', coordinates: [34.09,124.34] }};

Event.find({
  'geo': {
    $near: lonLat,
    $maxDistance: maxDistance
  }
}).exec(function(err, events) {
  if(err) {
    throw err;
  } else {
    return events;
  }
});

我在这里有什么语法错误?我错过了什么巨大的东西吗?除了 this link 之外,任何文档的任何 url 都很棒。

【问题讨论】:

标签: mongodb mongoose geospatial 2dsphere database


【解决方案1】:

你试过用这句话创建索引吗?

db.event.ensureIndex({ "location" : "2d" } )

您的示例有点令人困惑。我不知道“位置”是您的坐标还是“地理”,因为您使用前者创建架构并使用后者进行查询。

在这两种情况下,一个好主意是执行

db.event.findOne().pretty()

这样您就可以检查收藏的格式。

您还可以使用此命令检查当前索引。

db.event.getIndexes()

如果您正在处理“事件”集合,所有这些都有效。

【讨论】:

    猜你喜欢
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多