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条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件