【问题标题】:Mongo (+Mongoose) error: Unable to find index for $geoNear queryMongo (+Mongoose) 错误:无法找到 $geoNear 查询的索引
【发布时间】:2015-01-06 03:46:48
【问题描述】:

这是我的架构:

var ActivitySchema = new Schema({
    loc: {type: [Number], index: '2dsphere'}
});

当我尝试查找文件时:

Activity.find().where('loc').near({
        center: [-71.072810, 42.370227],
        spherical: true
    }).exec(function(err, docs){
        if (err) console.log(err);
        console.log(docs);
    })

这是我得到的:

{ [MongoError:无法执行查询:错误处理查询: ns=bunch-test.activities 限制=0 跳过=0 树:GEONEAR 字段=loc maxdist=1.79769e+308 isNearSphere=1 排序:{} 项目:{} 规划器 返回错误:无法找到 $geoNear 查询的索引] 名称: 'MongoError' }

我尝试过以多种方式定义 loc。例如:

loc: {
        type: {
            type: String,
            enum: 'Point',
            default: 'Point'
        },
        coordinates: {
            type: [Number],
            default: [0,0],
        }
    }

还有多种查询方式:

Activity.find({
        loc: {
            $nearSphere: {
                $geometry: {
                    type: "Point" ,
                    coordinates: [ -71.072810, 42.370227 ]
                },
                $minDistance: 0,
                $maxDistance: 10000,
            }
        }
    })

或:

Activity.geoNear({
   type: "Point" ,
   coordinates: [ -71.072810, 42.370227 ]
},
{ maxDistance : 99999999999,
  spherical : true 
},
function(err, results, stats) {
   console.log(results);
   console.log(err);
});

但结果总是相同的消息:

找不到 $geoNear 查询的索引

有什么建议吗?谢谢。

【问题讨论】:

    标签: node.js mongodb express mongoose geo


    【解决方案1】:

    问题仅在使用 Mocha 进行测试时发生。我的后(功能(完成)});在每个测试文件的末尾删除了整个数据库,并且没有为下一次测试再次创建索引。

    我通过将 mongoose.connection.db.dropDatabase 替换为 Model.remove({}, callback); 解决了这个问题;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2017-03-24
      • 2021-10-28
      相关资源
      最近更新 更多