Mongodb 如何查找距给定点一定距离内的所有文档

Mongodb 如何查找距给定点一定距离内的所有文档,mongodb,geospatial,Mongodb,Geospatial,给定以下用户集合(每个用户可能有多个地址) 。。。如何选择距离给定点不超过20公里的所有用户?。。。它不起作用,因为我忘了创建2dsphere索引: db.users.createIndex( { "addresses.location" : "2dsphere" } ) db.users.find( { "addresses.location": { "$near" : { "$geometry": { type: "

给定以下
用户
集合(每个用户可能有多个地址)


。。。如何选择距离给定点不超过20公里的所有用户?

。。。它不起作用,因为我忘了创建
2dsphere
索引:

db.users.createIndex( { "addresses.location" : "2dsphere" } )

db.users.find(
   {
     "addresses.location":
       { "$near" :
          {
            "$geometry": { type: "Point", "coordinates" : [ 8.952682495117188, 46.03413009643555 ] },
            "$maxDistance": 20000
          }
       }
   }
)
现在它工作了。。。我希望这能有所帮助

db.users.createIndex( { "addresses.location" : "2dsphere" } )

db.users.find(
   {
     "addresses.location":
       { "$near" :
          {
            "$geometry": { type: "Point", "coordinates" : [ 8.952682495117188, 46.03413009643555 ] },
            "$maxDistance": 20000
          }
       }
   }
)