【发布时间】:2021-06-28 16:29:43
【问题描述】:
我想根据其他字段的值向 mongo 集合中的所有文档添加新字段,前提是它们不为空。我已经编写了一个脚本来执行此操作,但是我们可以使用一个查询来完成此操作吗?
样本数据:
[{
"outdoor": {
"location": {
"type": "Point",
"coordinates": [
-92.41151,
35.11683
]
}
},
"address": { "street1" : "Street 1",
"postalCode" : "95050",
"state" : "CA",
"locality" : "City 1",
"countryCode" : "US"},
"customerId":"8047380094"
},
{
"outdoor": {
"location": {
"type": "Point",
"coordinates": [
-89.58342,
36.859161
]
}
},
"customerId":"8047380094"
},
{
"address": {
"street1" : "Street 1",
"postalCode" : "95050",
"state" : "CA",
"locality" : "City 1",
"countryCode" : "US"
},
"customerId":"8047380094"
}]
需要添加以下新字段:
- new_address - 如果地址数据不为空,则将地址数据复制到此字段中
- new_location - 如果outdoor.location 不为空,则将其复制到该字段中
感谢任何帮助。
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework