使用MongoDB将动态密钥处理成csv

时间:2014-10-14 11:41:20

标签: mongodb

输入文件是

 {
  "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等...

0 个答案:

没有答案