dongzhuo2010 2014-05-19 06:45
浏览 175
已采纳

mongodb在单个数组文档中插入字段

I have a document in mongodb that looks like this

{
   "_id": ObjectId("5378da275ad972a811c119fb"),
   "name": "test test",
   "fname": "test",
   "lname": "test",
   "phone": "13254355554",
   "user": "525518965ad972636d7aa0ae",
}

And i want to insert a new field for "employee_id" so the result should be exactly like this.

 {
   "_id": ObjectId("5378da275ad972a811c119fb"),
   "name": "test test",
   "fname": "test",
   "lname": "test",
   "employee_id": "09872",
   "phone": "13254355554",
   "user": "525518965ad972636d7aa0ae",
}

I have used $push and $addToSet but the results became an array like

"employee_id": {
     "0": "09872",
} 
  • 写回答

2条回答 默认 最新

  • dongqiu7365 2014-05-19 06:53
    关注

    Just use $set;

    db.test.update(
          { _id:ObjectId("5378da275ad972a811c119fb") },    // Update this id only 
          { $set:{"employee_id": "09872"} }                // by setting employee_id
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?