如何在mongodb中的同一集合中存储许多文档

时间:2015-04-05 09:29:23

标签: node.js mongodb mongoose

{
    "_id": {
        "$oid": ""
    },
    "email": "",
    "password": "",
    "profile": {
        "picture": "",
        "website": "",
        "location": "",
        "gender": "",
        "name": ""
    },
    "workdetails": [
     {
        "company_name": "",
        "job_title": "",
        "role": ""
    },
    {
        "company_name": "",
        "job_title": "",
        "role": ""
    },
}

如何使用nodejs为mongodb中的workdetails创建工作细节的模式并存储多个文档。请指导。

1 个答案:

答案 0 :(得分:0)

您可以将其声明为数组

workdetails: { type : Array , "default" : [] }

插入可以像

objectn.workdetails= [{
        "company_name": "",
        "job_title": "",
        "role": ""
    }, {
        "company_name": "",
        "job_title": "",
        "role": ""
    }];
yourschema.create(objectn, function (err, a, b) {
  if (err) // ...
});