我有一個檔案類別串列: documentCategory 我有一個檔案表
如何將 Document 中的單個檔案放入 documentCategory 中的 UI 子項
1、I have a list of document categories : documentCategory
{
"_id" : ObjectId("61cd249f50f3abf2e172d743"),
"name" : "Vue3",
"pid" : "-1",
"level" : 1,
"children" : [
{
"name" : "UI",
"pid" : "61cd249f50f3abf2e172d743",
"level" : 2,
"_id" : ObjectId("61cd27d1f3b969fec5458d89")
}
]
}
2、I have a document table
{
"_id" : ObjectId("61cd4813db3e3db388e1d7cf"),
"name" : "ElementUI",
"cid" : "61cd27d1f3b969fec5458d89",
"cname" : "UI",
"pid" : "61cd249f50f3abf2e172d743",
"pname" : "Vue3",
}
希望得到結果:
{
"_id" : ObjectId("61cd249f50f3abf2e172d743"),
"name" : "Vue3",
"pid" : "-1",
"level" : 1,
"children" : [
{
"name" : "UI",
"pid" : "61cd249f50f3abf2e172d743",
"_id" : ObjectId("61cd27d1f3b969fec5458d89"),
children:[
{name:'ElementUI'},{name:'**'}
]
}
]
}
如何將 Document 中的單個檔案放入 documentCategory 中的 UI 子項
uj5u.com熱心網友回復:
由于您試圖將 ObjectId 匹配到字串上,因此我必須添加 $set 階段以將它們轉換為相同的型別(字串)。
請注意,我命名了我的第二個集合表而不是表,因為最佳實踐是復數
db.collection.aggregate([
{
'$match': {
'name': 'Vue3'
}
}, {
'$unwind': {
'path': '$children'
}
}, {
'$set': {
'children._id': {
'$toString': '$children._id'
}
}
}, {
'$lookup': {
'from': 'tables',
'localField': 'children._id',
'foreignField': 'cid',
'as': 'children.children',
'pipeline': [
{
'$project': {
'name': 1,
'_id': 0
}
}
]
}
}, {
'$group': {
'_id': '$_id',
'name': {
'$first': '$name'
},
'pid': {
'$first': '$pid'
},
'level': {
'$first': '$level'
},
'children': {
'$push': '$children'
}
}
}
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399223.html
標籤:MongoDB 猫鼬 mongodb-查询 总计的 抬头
上一篇:TypeError:在從mongodb檢索html字串時將回圈結構轉換為JSON
下一篇:MongooseError:回呼必須是一個函式,用findOnemongoose得到[objectObject]
