输入文件是
{
"Includes": {
"Employees": {
"14": {
"name": "john",
"age": 12,
"activity": {
"Count": 3502,
"RatingValue": 5
}
},
"17": {
"name": "smith",
"age": 23,
"activity": {
"Count": 232,
"RatingValue": 5
}
}
}
}
}
使用硬编码键和商店处理名为"结果"的集合的函数。
db.details.find().forEach(function(doc) {
var Employees = doc.Includes.Employees;
if (Employees) {
for ( var key in Employees) {
var Employee = Employees[key];
if (Employee) {
var item = {
id : key,
name : Employee.name,
age : Employee.age
};
var activity = Employee.activity;
if (activity) {
item.Count = activity.Count;
item.RatingValue = activity.RatingValue;
}
db.result.insert(item);
}
}
}
});
是否可以插入集合"结果"动态没有硬编码,如Employee.name,Employee.age等...