【发布时间】:2022-01-27 15:51:30
【问题描述】:
我有一个文档,其中包含一个包含对象元素的数组属性。
例如
{
"_id": "61c01098bc29520008efc00f",
"name": "codedump",
"employments": [
{
"type": "PERMANENT",
"salary": 1000000000
},
{
"type": "TEMP",
"salary": 1000000000
},
{
"type": "PART_TIME",
"salary": 1000000000
},
]
}
并且在使用type 更新就业工资时,如果该类型已经在就业中,那么我只需要更新salary。否则我需要将新项目推送到数组中。我正在尝试找到一种方法在单个查询中完成这项工作。
- 如果该类型已经存在于数组中(用于更新):
/// Update data:
{
"type": "PERMANENT",
"salary": 10
}
// For this case I want the example document to become:
{
"_id": "61c01098bc29520008efc00f",
"name": "codedump",
"employments": [
{
"type": "PERMANENT",
"salary": 10 // From 1000000000 to 10
},
{
"type": "TEMP",
"salary": 1000000000
},
{
"type": "PART_TIME",
"salary": 1000000000
},
]
}
- 如果类型不存在:
/// Update data:
{
"type": "INVESTMENT",
"salary": 10000
}
// For this case I want the example document to become:
{
"_id": "61c01098bc29520008efc00f",
"name": "codedump",
"employments": [
{
"type": "PERMANENT",
"salary": 1000000000
},
{
"type": "TEMP",
"salary": 1000000000
},
{
"type": "PART_TIME",
"salary": 1000000000
},
{
"type": "INVESTMENT",
"salary": 10000
}
]
}
【问题讨论】:
标签: node.js arrays mongodb aws-lambda mongodb-query