无法在Mongoose / MongoDB中使用坐标$ near语句find附近位置的结果
我正试图从一个特定的点获得附近的位置的结果。 通过使用ID查找的正常方式,返回的结果,但是当我试图find使用坐标附近的位置,没有结果出现。 在我的代码中没有任何错误/警告,但也没有任何结果。 我错过了什么?
模型模式
var locationSchema = new Schema({ fb_location: String, coordinates:[] }); var feed_postSchema = new Schema({ content: String, location: [locationSchema], image: [{ type : String }], created_at: { type : Date, default: Date.now } }); locationSchema.index({coordinates:'2dsphere'});
Server.js
app.get('/test_radar',function(request,response){ Feed_Post.find( { 'location.coordinates' : { $near : { coordinates: [100,5] }, $maxDistance: 100 } },function(err,ret){ console.log(response.json(ret)); }) });
示例数据库数据
"_id" : ObjectId("5620a3c2fde01a55065f4c3b"), "content" : "Test post", "created_at" : ISODate("2015-10-16T07:14:10.636Z"), "image" : [ ], "location" : [ { "fb_location" : "Malaysia", "_id" : ObjectId("5620a3c2fde01a55065f4c3c"), "coordinates" : [ 100, 5 ] } ], "__v" : 0
注意:我确实将$ maxDistance设置为100 * 1000,但仍然没有结果。
$near
操作符需要一个地理空间索引。
如果要使用旧坐标将您的位置定义为点,则必须创build二维索引 。 那么你将能够在你指定的语法中使用$near
操作符。
您创build了2dsphere索引,因此您必须在查询中指定GeoJSON点:
Feed_Post.find( { 'location.coordinates' : { $geometry: { type: "Point" , coordinates: [ 100 , 5 ] }, $maxDistance: 100 } },function(err,ret){ console.log(response.json(ret)); })
检查$附近的定义。
- 将XXX.db.bson_serializer.ObjectID.createFromHexString(id)外包给函数?
- mongodb增量编号 – 500内部服务器错误
- Mongoose:将mongocollections的date字段增加一个月
- Mongoose或MongoDB的架构定义?
- 给Derby.js或Meteor一个用于authentication生产的应用程序是否已经是值得的了?
- 在节点上用于mongo的纯JavaScript客户端?
- 有没有办法在Mongoose中进行投影?
- TypeError:callback.apply不是函数(Node.js&Mongodb)
- Mongoose模型值作为string而不是对象返回