美文网首页
mongodb-geoWithin查询、geoNear查询

mongodb-geoWithin查询、geoNear查询

作者: 威研威语 | 来源:发表于2017-01-08 22:52 被阅读0次

    - 使用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查询

          本文链接:https://www.haomeiwen.com/subject/jpjcbttx.html