如何在mongodb中处理geolocation(point)对象中的空值?

时间:2017-12-09 11:44:43

标签: mongodb null geospatial point

我目前正在使用mongodb,其中包含具有以下结构的公司集合:

{
    "_id": "52cdef7c4bab8bd675297d8e",
    "name": "Facebook",
    "offices": [{
        "description": "Headquarters",
        "address1": "1601 Willow Road",
        "address2": "",
        "zip_code": "94025",
        "city": "Menlo Park",
        "state_code": "CA",
        "country_code": "USA",
        "latitude": 37.41605,
        "longitude": -122.151801,
        "location": {
            "type": "Point",
            "coordinates": [-122.151801, 37.41605]
        }
    }, {
        "description": "Europe HQ",
        "address1": "",
        "address2": "",
        "zip_code": "",
        "city": "Dublin",
        "state_code": null,
        "country_code": "IRL",
        "latitude": 53.344104,
        "longitude": -6.267494,
        "location": {
            "type": "Point",
            "coordinates": [-6.267494, 53.344104]
        }
    }, {
        "description": "New York",
        "address1": "340 Madison Ave",
        "address2": "",
        "zip_code": "10017",
        "city": "New York",
        "state_code": "NY",
        "country_code": "USA",
        "latitude": null,
        "longitude": null,
        "location": {
            "type": "Point",
            "coordinates": [null, null]
        }
    }]
}

我想在位置字段上创建地理空间索引:

db.startups.createIndex( { "offices.location" : "2dsphere" } )

但是,某些办公室的经度和纬度是空值,因此索引创建失败并显示Point must only contain numeric elements

我的问题是我应该和/或如何处理这个问题。

1 个答案:

答案 0 :(得分:1)

只需删除违规元素的整个位置:

"location": {
    "type": "Point",
    "coordinates": [null, null]
}