【问题标题】:Update array of particular object based on id and Objectid根据 id 和 Objectid 更新特定对象的数组
【发布时间】:2019-01-09 15:52:44
【问题描述】:

我的数据库结构

_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) 更新属性

谁能帮忙。 提前致谢。

【问题讨论】:

  • 你用的是mongodb还是mongoose?
  • 请贴出您尝试过的代码。你使用的是 mongodb 还是 mongoose?
  • 我正在使用猫鼬。我试过这个。但它正在更新 authorId -----> db.myCollection.update({_id:ObjectId("123456789")},{$set:{"authors":[{authorId:456},{authorname:"dublin" }]}})
  • 我在这里传递 (_id) 但我不知道如何传递 authorId。

标签: node.js mongodb


【解决方案1】:

我试过这个。它对我有用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多