【问题标题】:Including records with empty location field in mongoose near query在 mongoose near 查询中包含具有空位置字段的记录
【发布时间】:2017-09-09 23:03:37
【问题描述】:

我一直在尝试创建一个 mongoose 模型,其中我可以有一个 geojson 位置字段并根据位置查询记录,但我也希望自动包含未设置位置字段的记录。

我什至将 sparse 条件设置为 true 以确保我可以创建空位置字段,但调用 $near 查询会产生此错误:

unable to find index for $geoNear query

我试图找到有关如何设计此查询的答案,不胜感激。

这是我的模型:

var EventSchema = new mongoose.Schema({
  loc: {
    "type": { type: String, default: 'Point' },
    coordinates: [Number]
  },
  pos: Number,
  active: Boolean
});
EventSchema.index({ loc: '2dsphere' }, { sparse: true });

这是查询:

exports.index = function (req, res, next) {
    Event
      .find({ active: true })
      .near('loc', { center: point, spherical: true, maxDistance: 5000,   distanceMultiplier: 6378.1 })
      .sort('pos')
      .limit(6)
      .lean(true)
      .exec(function (err, events) {
      if(!events) {
        res.status(400).json(err || {status: false, message: 'No Events'});
      }
      else
        res.status(200).json(events);
    });
}

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    试试你的架构:

    var EventSchema = new mongoose.Schema({ loc: { "type": [ Number ] , index: '2dsphere' } , pos: Number , active: Boolean });

    【讨论】:

      猜你喜欢
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      相关资源
      最近更新 更多