Mongodb node.js组织的数组结果 [英] Mongodb node.js organised array result
本文介绍了Mongodb node.js组织的数组结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理当前结果为
{ "employee_details" : [{
"name" : "Treasure Bliss Dwayne",
"work_details" : [{
"shift" : [ "spray", "smoothing", "packaging", "shining"],
"worktime" : [ [ 5 ], [ ], [ 8 ], [ 10 ]] }
]}
]}
我一直在寻找产生以下输出的方法
I have been looking for ways of producing the following output
{ "employee_details" :
[
{ "name" : "Treasure Bliss Dwayne"},
{ "spray":5},
{"smoothing":},
{"packaging":8},
{"shining":10}
]
}
请问我该怎么办
推荐答案
您可以在下面的聚合中使用
You can use below aggregation
db.collection.aggregate([
{ "$project": {
"employee_details": {
"$map": {
"input": "$employee_details",
"as": "emp",
"in": {
"name": "$$emp.name",
"work_details": {
"$map": {
"input": "$$emp.work_details",
"as": "aa",
"in": {
"$arrayToObject": {
"$map": {
"input": { "$range": [0, { "$size": "$$aa.shift" }] },
"as": "qq",
"in": {
"k": { "$arrayElemAt": ["$$aa.shift", "$$qq"] },
"v": {
"$ifNull": [
{
"$arrayElemAt": [
{ "$arrayElemAt": ["$$aa.worktime", "$$qq"] },
0
]
},
""
]
}
}
}
}
}
}
}
}
}
}
}}
])
这篇关于Mongodb node.js组织的数组结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文