mongodb - mongodb insert 属性顺序重要吗?

标签 mongodb insert geojson 2dsphere

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

[
    {
        "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手册,2dsphere索引有一些字段限制。限制如下:

2dsphere Indexed Field Restrictions Fields with 2dsphere indexes must hold geometry data in the form of coordinate pairs or GeoJSON data. If you attempt to insert a document with non-geometry data in a 2dsphere indexed field, or build a 2dsphere index on a collection where the indexed field has non-geometry data, the operation will fail.

要成为 GeoJSON 对象,对象应根据以下结构包含 类型坐标 字段:

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

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

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

关于mongodb - mongodb insert 属性顺序重要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30490002/

相关文章:

node.js - moment(new Date().toISOString() 始终返回相同的日期和时间

python - 将值从一个键转移到python字典中的另一个键

MongoDB 和 "joins"

java - 使用 MongoDB Java 3.0 驱动程序批量更新

MongoDB 突然丢失/找不到数据

php - INSERT 语句在 PHP 脚本中默默失败,但在提示符下有效

SQL 通过连接插入值

javascript - NodeJS Mongoose - 无法调用未定义的方法 'toString'

leaflet - GeoJSON MultiPoint 未显示在 map 上

javascript - 如何知道给定的 latlng 是否在 geojson 多边形坐标内