如何处理 mongodb 中地理位置(点)对象中的空值?



我目前正在使用一个包含具有以下结构的公司集合的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

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

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

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

最新更新