Mongodb嵌套插入

时间:2016-08-25 10:27:10

标签: mongodb nested

我的数据如下,现在,favoriteapi将有关键/值记录进出。

如何将数据插入此嵌套元素

someHandler()

}

我试过这个

{

"firstname": "ram5656",
"lastname": "rahul",
"email": "ram@ram.com",
"password": "encrypt",
"favoriteapi" : [ 
    {
        "api_id" : "function",
        "api_name" : "enterprise"
    }, 
    {
        "api_id" : "data_subject_category",
        "api_name" : "address"
    }, 
    {
        "api_id" : "data_subject",
        "api_name" : "address"
    }
],
"is_active": true,
"create_ts": "2016-05-18T00:00:00.000Z",
"create_by": "ram@ram.com",
"update_ts": "2016-05-18T06:00:00.000Z",
"update_by": "ram@ram.com",
"lastlogin": "2016-05-01T00:00:00.000Z"

但是,没有运气。

我应该使用upsert吗?

1 个答案:

答案 0 :(得分:0)

您必须在用户文档上执行update,并指定要{em>

var updatedocument = {
    "api_id" : "sanket",
    "api_name" : "sanket"
};

db.user.update(
  {"email": "ram@ram.com"},
  {$push: {
      "favouriteapi" : updatedocument
    }
  }
);