search

Home  >  Q&A  >  body text

mongodb 文档更新

新手请教mongodb更新文档。
现有collection名为 user的数据如下
{

name:"abc",
people:{
       student:{
           stu_name:"YYW",
           stu_num:12345
       }     
}

}

现在我要在people.student下添加一个名为stu_sex的字段,请问用什么样的语句,新手第一次接触mongodb,谢谢指教。

为情所困为情所困2867 days ago606

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-02 09:24:15

    Suppose there is only such a piece of data in your collection, a feasible code is as follows

    db.user.update({name: 'abc'}, {$set: {'people.student.stu_sex': 'male'}});

    If your data actually has the primary key _id, replace the above

    {name: 'abc'}

    Replace with the corresponding selector

    reply
    0
  • Cancelreply