根据id和Objectid更新特定对象的数组

时间:2019-01-09 07:32:41

标签: node.js mongodb

  

我的数据库结构

_id:ObjectId("123456789")
name:"prudhvi"
authors:Array
0:Object
authorId:123
authorname:"venkat"
department:software
1:Object
authorId:456
authorname:"venkat"
department:hardware

我的数据库结构是这样的。我需要更新1:Object,我正在尝试根据authorId和userid(_id)更新属性。

任何人都可以帮忙。 预先感谢。

1 个答案:

答案 0 :(得分:0)

我尝试过了。它对我有用

db.myCollection.update({_id:ObjectId("123456789"),authors:{$elemMatch:{authorId:456}}},{$set:{"authors.$.authorname":"prudhvi"}})