mongoose 使用 地理空间索引例子
发布于 3 个月前 作者 zmzkkk 339 次浏览 来自 问答

求一个 mongoose 使用 地理空间索引例子

2 回复

你自己查文档啊,毕竟这个用的少 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; } }); };

伸手党这样不太好吧

回到顶部