根据其他字段将新字段添加到 collection
Add new field to a collection based on other fields
"MongoDB 版本: 4.2
OS: OSX
我在主页中有以下文档 collection:
{
"_id" : "xxxx",
"name" : "home1",
"homeName" : "home1",
"allowAdminActions" : true,
"_created_at" : ISODate("2020-02-07T14:55:06.819Z"),
"_updated_at" : ISODate("2020-02-07T14:55:12.356Z"),
"_p_Owner" : "xxxxx",
"allowUpdate" : false,
"devicesConfig" : {
"hello" : 1,
"deviceConfig" : {
"data" : [
{
"type" : "CommunicationConfig",
"wifiName" : "wifitest",
"wifiPassword" : "wifipassword"
}
]
}
}
}
然后我想创建一个名为 wifiName
的字段,从 devicesConfig
字段推断:
db.Home.update(
{},
[
{"$project": {"connConfig": {$arrayElemAt: ["$devicesConfig.deviceConfig.data", 0]}}},
{"$set": {"wifiName": "$connConfig.wifiName"}}
],
false,
true
)
预期结果为:
{
"_id" : "xxxx",
"name" : "home1",
"homeName" : "home1",
"allowAdminActions" : true,
"_created_at" : ISODate("2020-02-07T14:55:06.819Z"),
"_updated_at" : ISODate("2020-02-07T14:55:12.356Z"),
"_p_Owner" : "xxxx",
"allowUpdate" : false,
"devicesConfig" : {
"hello" : 1,
"deviceConfig" : {
"data" : [
{
"type" : "CommunicationConfig",
"wifiName" : "wifitest",
"wifiPassword" : "wifipassword"
}
]
}
},
"wifiName" : "wifitest"
}
实际结果:
{
"_id" : "xxxxx",
"connConfig" : {
"type" : "CommunicationConfig",
"wifiName" : "testwifi",
"wifiPassword" : "testpassword"
},
"wifiName" : "testwifi"
}
如何避免 update
操作删除所有其他 non-specified 字段?
谢谢!
你应该使用 $addFields with combination of $project :
db.Home.update(
{},
[
{"$addFields": {"connConfig": {$arrayElemAt: ["$devicesConfig.deviceConfig.data", 0]}}},
{"$set": {"wifiName": "$connConfig.wifiName"}},
{'$project' : {'connConfig': 0}}
],
false,
true
)
"MongoDB 版本: 4.2 OS: OSX
我在主页中有以下文档 collection:
{
"_id" : "xxxx",
"name" : "home1",
"homeName" : "home1",
"allowAdminActions" : true,
"_created_at" : ISODate("2020-02-07T14:55:06.819Z"),
"_updated_at" : ISODate("2020-02-07T14:55:12.356Z"),
"_p_Owner" : "xxxxx",
"allowUpdate" : false,
"devicesConfig" : {
"hello" : 1,
"deviceConfig" : {
"data" : [
{
"type" : "CommunicationConfig",
"wifiName" : "wifitest",
"wifiPassword" : "wifipassword"
}
]
}
}
}
然后我想创建一个名为 wifiName
的字段,从 devicesConfig
字段推断:
db.Home.update(
{},
[
{"$project": {"connConfig": {$arrayElemAt: ["$devicesConfig.deviceConfig.data", 0]}}},
{"$set": {"wifiName": "$connConfig.wifiName"}}
],
false,
true
)
预期结果为:
{
"_id" : "xxxx",
"name" : "home1",
"homeName" : "home1",
"allowAdminActions" : true,
"_created_at" : ISODate("2020-02-07T14:55:06.819Z"),
"_updated_at" : ISODate("2020-02-07T14:55:12.356Z"),
"_p_Owner" : "xxxx",
"allowUpdate" : false,
"devicesConfig" : {
"hello" : 1,
"deviceConfig" : {
"data" : [
{
"type" : "CommunicationConfig",
"wifiName" : "wifitest",
"wifiPassword" : "wifipassword"
}
]
}
},
"wifiName" : "wifitest"
}
实际结果:
{
"_id" : "xxxxx",
"connConfig" : {
"type" : "CommunicationConfig",
"wifiName" : "testwifi",
"wifiPassword" : "testpassword"
},
"wifiName" : "testwifi"
}
如何避免 update
操作删除所有其他 non-specified 字段?
谢谢!
你应该使用 $addFields with combination of $project :
db.Home.update(
{},
[
{"$addFields": {"connConfig": {$arrayElemAt: ["$devicesConfig.deviceConfig.data", 0]}}},
{"$set": {"wifiName": "$connConfig.wifiName"}},
{'$project' : {'connConfig': 0}}
],
false,
true
)