[英]Merge document to an array field using mongodb aggregation
I have a collection like;我有一个像这样的集合;
{
"_id": ObjectId("5f8069b848e54248f8302b18"),
"topics": [{
"_id": "123",
"name": "ABC",
"type": 0
}, {
"_id": "455",
"name": "DFR",
"type": 0
}
],
"topic": {
"_id": "777",
"name": "FFG",
"type": 123
}
}
Ho can I write an aggregation query to merge topic to topics?我可以编写一个聚合查询来将主题合并到主题吗?
Is topic always unique?主题总是独一无二的吗? (Ie: not already included in topics) If so you can just add it to the array. (即:尚未包含在主题中)如果是这样,您可以将其添加到数组中。
https://mongoplayground.net/p/vKv_qjm3VNp https://mongoplayground.net/p/vKv_qjm3VNp
db.collection.aggregate([
{
"$project": {
topics: {
"$concatArrays": [
"$topics",
[
"$topic"
]
]
}
}
}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.