美文网首页
地理位置索引-2d索引

地理位置索引-2d索引

作者: 威研威语 | 来源:发表于2017-01-07 16:04 被阅读0次

    ```

    > db.location.ensureIndex({"w":"2d"})

    {

    "createdCollectionAutomatically" : true,

    "numIndexesBefore" : 1,

    "numIndexesAfter" : 2,

    "ok" : 1

    }

    > db.location.insert({w:[1,1]})

    WriteResult({ "nInserted" : 1 })

    > db.location.insert({w:[1,2]})

    WriteResult({ "nInserted" : 1 })

    > db.location.insert({w:[3,2]})

    WriteResult({ "nInserted" : 1 })

    > db.location.insert({w:[100,100]})

    WriteResult({ "nInserted" : 1 })

    > db.location.insert({w:[200,100]})

    WriteResult({

    "nInserted" : 0,

    "writeError" : {

    "code" : 13027,

    "errmsg" : "point not in interval of [ -180, 180 ] :: caused by :: { _id: ObjectId('5870915a9a0bf268727ed5c0'), w: [ 200.0, 100.0 ] }"

    }

    })

    > db.location.insert({w:[180,100]})

    WriteResult({ "nInserted" : 1 })

    > db.location.remove({w:[180,100]})

    WriteResult({ "nRemoved" : 1 })

    > db.location.insert({w:[180,80]})

    WriteResult({ "nInserted" : 1 })

    > db.location.find({w:{$near:[1,1]}})

    { "_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:{$near:[1,1],$maxDistance:10}})

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

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

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

    > db.location.find({w:{$near:[1,1],$maxDistance:10,$minDistance:1}})

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

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

    > db.location.find({w:{$near:[1,1],$maxDistance:10,$minDistance:2}})

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

    > db.location.find({w:{$near:[1,1],$maxDistance:10,$minDistance:3}})

    >

    ```

    相关文章

      网友评论

          本文标题:地理位置索引-2d索引

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