如何将nodejs中的以下对象数组保存到mongodb?

时间:2017-07-11 10:33:43

标签: node.js mongodb

我想将以下数据保存到mongodb中。我该怎么做?

{
 "resourceID":"5963647f2bf14038f04d4f01",
 "privilegeID":["596363222bf14038f04d4efd","5963648b2bf14038f04d4f02","596364942bf14038f04d4f03","596364a02bf14038f04d4f04"]
}

2 个答案:

答案 0 :(得分:0)

export function _create(req) {
  let resourceID = req.body.resourceID;
  let resourcePrivilegeEntries = [];
  req.body.privilegeID.forEach(function (privilegeID, index) {
    resourcePrivilegeEntries.push({resourceID: resourceID, privilegeID: privilegeID})
  });
  return ResourcePrivilege.collection.insert(resourcePrivilegeEntries);
}

答案 1 :(得分:0)

[
    {
        "_id": "5964ccebf80f382524a5b514",
        "resourceID": "5963647f2bf14038f04d4f01",
        "privilegeID": "596363222bf14038f04d4efd"
    },
    {
        "_id": "5964ccebf80f382524a5b515",
        "resourceID": "5963647f2bf14038f04d4f01",
        "privilegeID": "5963648b2bf14038f04d4f02"
    },
    {
        "_id": "5964ccebf80f382524a5b516",
        "resourceID": "5963647f2bf14038f04d4f01",
        "privilegeID": "596364942bf14038f04d4f03"
    },
    {
        "_id": "5964ccebf80f382524a5b517",
        "resourceID": "5963647f2bf14038f04d4f01",
        "privilegeID": "596364a02bf14038f04d4f04"
    }
]