【发布时间】: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