【问题标题】:mongoldb insert does the property order matters?mongodb insert 属性顺序重要吗?
【发布时间】:2015-05-27 18:06:23
【问题描述】:

我有一个包含以下索引的集合。

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "bs.locations"
    },
    {
        "v" : 1,
        "key" : {
            "location" : "2dsphere"
        },
        "name" : "location_2dsphere",
        "ns" : "bs.locations",
        "2dsphereIndexVersion" : 2
    }
]

我可以插入以下文档:

db.locations.insert({ "location" : {"coordinates" : [ 6.982654547382455, 46.88414220428685 ], "type" : "Point", "test":1.0} })

但是当我尝试插入这个文档时:

db.locations.insert({ "location" : {"test":1.0, "coordinates" : [ 6.982654547382455, 46.88414220428685 ], "type" : "Point"} })

我收到以下错误:

WriteResult({
    "nInserted" : 0,
    "writeError" : {
        "code" : 16755,
        "errmsg" : "Can't extract geo keys: { _id: ObjectId('5566050507c10c31ce7214af'), location: { test: 1.0, coordinates: [ 6.982654547382455, 46.88414220428685 ], type: \"Point\" } }  Point must only contain numeric elements"
    }
})

我的问题是,我在这里想念什么?我的错误是什么?

我使用的是 MongoDB v3.0.1 版本

【问题讨论】:

    标签: mongodb insert geojson 2dsphere


    【解决方案1】:

    根据 MongoDB 手册,2dsphere 索引有一些字段限制。限制如下:

    2dsphere 索引字段限制 具有 2dsphere 索引的字段必须 以坐标对或 GeoJSON 数据的形式保存几何数据。如果 您尝试在 2dsphere 中插入包含非几何数据的文档 索引字段,或在集合上构建 2dsphere 索引,其中 索引字段有非几何数据,操作将失败。

    要成为GeoJSON 对象,对象应根据以下结构包含typecoordinates 字段:

    { type: "<GeoJSON type>" , coordinates: <coordinates> }
    

    由于location 被索引为2dsphere,您的第一个插入查询会得到满足,因为它包含文档开头的对。但是在第二种情况下,文档结构违反了限制。它从一开始就需要 typecoordinates

    根据 MongoDB 手册,这是您的插入语句情况背后的唯一合乎逻辑的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-10
      • 2014-08-06
      • 2018-01-09
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多