2015-10-16 57 views
1

我想从特定点获取附近位置的结果。通过使用ID查找的正常方式,返回结果,但是当我尝试使用坐标查找附近位置时,没有出现结果。我的代码中没有任何错误/警告,但也没有任何结果。我错过了什么?

模型模式

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目前仍没有结果。

回答

1

$near运算符需要一个地理空间索引。

如果要使用旧坐标将点定义为点,则必须有2D index created。然后,您将能够按照您指定的语法使用$near运算符。

您创建2dsphere指数,所以你必须在你的查询中指定以GeoJSON点:

Feed_Post.find( 
    { 
     'location.coordinates' : 
     { 
      $geometry: { 
       type: "Point" , 
       coordinates: [ 100 , 5 ] 
      }, 
      $maxDistance: 100 
     } 
    },function(err,ret){ 
     console.log(response.json(ret)); 
    }) 

检查$near定义。

+0

是的你是对的!我注意到我的错误。感谢你们对我的帮助! –

相关问题