如何更新mongodb中字段的值?

时间:2015-11-13 01:31:52

标签: javascript mongodb mongoose

我在mongodb中有如下文档:

{
 "_id":"563f480fb5c2187503eefc1a",
 "ownerBy":"56335521f2f4a3150349d78d",
 "data":   {
   "username1":"76561198060520554",
   "username2":"76561198060520554",
   "username3":"76561198060520554",
   "username4":"76561198060520554",
   "username5":"76561198060520554"
  }, 
 "__v":0
}

客户希望将username1的值更新为以下newValue

{ newValue: 'as76561198060520554',
  id: 'username1',
  column: 'selectable' 
}

如何找到字段_id并将其替换为newValue

1 个答案:

答案 0 :(得分:0)

鉴于我们掌握的信息很少,这可能是您正在寻找的信息!

收藏将是您收藏的名称!

Collection.update({
"_id":"563f480fb5c2187503eefc1a"
},
{$rename: {"_id" : "newValue"}},
{$set: { 
newValue: 'as76561198060520554',

id: 'username1',
column: 'selectable' 
}},
{"upsert": true});