求一个 mongoose 使用 地理空间索引例子
你自己查文档啊,毕竟这个用的少 var place = new Schema({ … creationDate: { type: Date, default: Date.now }, userId: ObjectId, username: String, geo: {type: [Number], index: ‘2d’} … }); exports.PlaceService = mongoose.model(‘Place’, place);
place.methods.findNear = function(cb) { return this.model(‘Place’).find({geo: { $nearSphere: this.geo, $maxDistance: 0.01} }, cb); }
exports.nearPlaces = function(req, res) { //I’m just sending the geolocation points in a format like this 39.92,-23 // but you can send it however you want var coord = req.query.geo.split(’,’); var Place = mongoose.model(‘Place’); var park = new Place({geo: coord}); place.findNear( function(err,docs) { if (!err) { res.json(docs); } else { throw err; } }); };
伸手党这样不太好吧
最近也在用这个,遇到问题如下,入库时直接报错: