arrays - 如何在 MongoDB 聚合中替换数组的值?
问题描述
我试图仅在聚合结果中替换值,而不是在存储的文档中,这就是我使用聚合而不是更新的原因。
假设这些是存储在集合中的文档:
{ "_id": 1, "first_name": "foo1", items: [
{ "item": "gloves", "body": "hands" },
{ "item": "gloves", "body": "hands" },
{ "item": "shirts" , "body": "torso" }
]
},
{ "_id": 2, "first_name": "foo2", items: [
{ "item": "skirts", "where": "legs" },
{ "item": "jeans", "body": "legs" },
{ "item": "socks" , "body": "feet" },
{ "item": "gloves" , "body": "hands" }
]
},
{ "_id": 3, "first_name": "foo3", items: [
{ "item": "jacket", "where": "torso" },
{ "item": "socks", "body": "feet" },
{ "item": "shirts" , "body": "torso" }
]
}
聚合必须用“sandals”代替“gloves”,并将“body”从“hands”改为“feet”。预期结果是:
{ "_id": 1, "first_name": "foo1", items: [
{ "item": "sandals", "body": "feet" },
{ "item": "sandals", "body": "feet" },
{ "item": "shirts" , "body": "torso" }
]
},
{ "_id": 2, "first_name": "foo2", items: [
{ "item": "skirts", "where": "legs" },
{ "item": "jeans", "body": "legs" },
{ "item": "socks" , "body": "feet" },
{ "item": "sandals" , "body": "feet" }
]
},
{ "_id": 3, "first_name": "foo3", items: [
{ "item": "jacket", "where": "torso" },
{ "item": "socks", "body": "feet" },
{ "item": "shirts" , "body": "torso" }
]
}
到目前为止,我已经尝试使用 "$unwind" 和 $project 和 "$switch" 以及其他方法,但它们都不起作用。
解决方案
推荐阅读
- macos - 从 Windows 迁移到 Mac:Podfile 安装出错
- python - 在熊猫的两列上分组后应用合并
- css - 在 /div wordpress 主题主页中隐藏#text
- crystal-reports - Crystal Reports 2016:如何在呈现组页脚时抑制页脚?
- javascript - 任何人都可以用javascript解释这段代码吗
- django - 错误“int”对象没有属性“get”
- oracle - 我正在尝试编写一个取消预订的 PL/SQL 过程。我不断收到关于列名的标识符错误
- angular - 在 Angular 9 中,我可以调整 forkJoin 以便在收到结果时对其进行处理吗?
- python - Python Append 在嵌套循环中生成的数据帧
- java - Java中的UCanAccess在带有特殊字符的列中使用ORDER BY子句返回错误的顺序