mongodb-geoWithin查询、geoNear查询

- 使用geoWithin查询

```

> db.location.find()

{ "_id" : ObjectId("587091389a0bf268727ed5bc"), "w" : [ 1, 1 ] }

{ "_id" : ObjectId("5870913c9a0bf268727ed5bd"), "w" : [ 1, 2 ] }

{ "_id" : ObjectId("587091419a0bf268727ed5be"), "w" : [ 3, 2 ] }

{ "_id" : ObjectId("587091509a0bf268727ed5bf"), "w" : [ 100, 100 ] }

{ "_id" : ObjectId("587091979a0bf268727ed5c2"), "w" : [ 180, 80 ] }

> db.location.find({w:{$geoWithin:{$box:[[0,0],[3,3]]}}})

{ "_id" : ObjectId("587091389a0bf268727ed5bc"), "w" : [ 1, 1 ] }

{ "_id" : ObjectId("5870913c9a0bf268727ed5bd"), "w" : [ 1, 2 ] }

{ "_id" : ObjectId("587091419a0bf268727ed5be"), "w" : [ 3, 2 ] }

> db.location.find({w:{$geoWithin:{$box:[[1,1],[2,3]]}}})

{ "_id" : ObjectId("587091389a0bf268727ed5bc"), "w" : [ 1, 1 ] }

{ "_id" : ObjectId("5870913c9a0bf268727ed5bd"), "w" : [ 1, 2 ] }

> db.location.find({w:{$geoWithin:{$center:[[0,0],5]}}})

{ "_id" : ObjectId("587091389a0bf268727ed5bc"), "w" : [ 1, 1 ] }

{ "_id" : ObjectId("5870913c9a0bf268727ed5bd"), "w" : [ 1, 2 ] }

{ "_id" : ObjectId("587091419a0bf268727ed5be"), "w" : [ 3, 2 ] }

> db.location.find({w:{$geoWithin:{$polygon:[[0,0],[0,1],[2,5],[6,1]]}}})

{ "_id" : ObjectId("587091389a0bf268727ed5bc"), "w" : [ 1, 1 ] }

{ "_id" : ObjectId("5870913c9a0bf268727ed5bd"), "w" : [ 1, 2 ] }

{ "_id" : ObjectId("587091419a0bf268727ed5be"), "w" : [ 3, 2 ] }

```

- geoNear查询

```

> db.runCommand({geoNear:"location",near:[1,2],maxDistance:10,num:1})

{

"results" : [

{

"dis" : 0,

"obj" : {

"_id" : ObjectId("5870913c9a0bf268727ed5bd"),

"w" : [

        1,

        2

        ]

            }

    }

],

"stats" : {

"nscanned" : 3,

"objectsLoaded" : 1,

"avgDistance" : 0,

"maxDistance" : 0,

"time" : 7

},

"ok" : 1

}

```

你可能感兴趣的:(mongodb-geoWithin查询、geoNear查询)