arrays - 更新深层嵌套数组 mongodb
问题描述
我想使用 mongodb 在附件数组中推送一个值。我想使用以下条件更新查询。
_id:ObjectId("5b56bd2f3e18580edc85af73") "cardID": ObjectId("5b56c895d0a04836f71aa776") "commentId":"2"
我想在附件中推动价值,任何帮助将不胜感激
这是一个集合对象:
{
"_id" : ObjectId("5b56bd2f3e18580edc85af73"),
"orgId" : "90",
"createdBy" : "test",
"name" : "testname",
"Cards" : [
{
"cardID" : ObjectId("5b56c895d0a04836f71aa776"),
"cardName" : "test Name",
"cardCreated" : "",
"reviewer" : "",
"priority" : "",
"cardPosition" : "",
"membersAssigned" : [
"ggg",
"fff"
],
"labels" : [
"l1",
"l2"
],
"description" : "",
"attachements" : [],
"comments" : [
{
"commentId" : "2",
"commentedBy" : "test",
"date" : "",
"comment" : "Hello world",
"attachements" : [
"1",
"data"
],
"emojis" : [
":smile:",
":thumbsup:"
],
"updatedBy" : "arkadata",
"updatedOn" : "",
"subComments" : {
"commentedBy" : "jaril",
"date" : "",
"comment" : "Hello world inside dark"
}
},
{
"commentId" : "3",
"commentedBy" : "test",
"date" : "",
"comment" : "Hello world",
"attachements" : [
"1",
"raj"
],
"emojis" : [
":smile:",
":thumbsup:"
],
"updatedBy" : "arkadata",
"updatedOn" : "",
"subComments" : {
"commentedBy" : "jaril",
"date" : "",
"comment" : "Hello world inside dark"
}
},
{
"commentId" : 6.0
}
],
"dueDate" : "",
"createdDate" : "",
"lastUpdated" : "",
"checkList" : [],
"position" : "5",
"status" : "active"
},
"timestamp" : ISODate("2018-07-24T05:46:23.890Z")
}
解决方案
您可以尝试使用 mongodb 3.6arrayFilters
db.collection.update(
{ "_id": ObjectId(5b56bd2f3e18580edc85af73) },
{ "$push": { "Cards.$[card].comments.$[comment].attachments": "2" } },
{ "arrayFilters": { "card.cardID": ObjectId("5b56c895d0a04836f71aa776"), "comment.commentId": 2 } }
)
确保将您的 id 转换为 ObjectId
编辑:
db.collection.update(
{ "_id": ObjectId(5b56bd2f3e18580edc85af73) },
{ "$push": { "Cards.$[card].comments.$[comment].attachments": "2" } },
{ "arrayFilters": [
{ "card.cardID": ObjectId("5b56c895d0a04836f71aa776")},
{"comment.commentId": 2 }
]
}
)
推荐阅读
- angular - 只有 Visual Studio Code 无法识别“ng”命令
- nativescript - Nativescript 调试安装包失败
- monitoring - GKE Stackdriver Monitoring 和使用 BigQuery 计量 GKE 使用量之间的区别
- c# - .Net 应用程序:数据库连接最佳实践
- android - 将两个项目限制在线性布局内的顶部和底部
- macos - 通过 macOS 本机应用程序中的 SwiftUI WKWebView 调用时,如何使文件打开对话框正常工作?
- database - 如何在页面加载时从代码后面设置模板字段标签文本属性?
- javascript - 使用 Ecmascript 6 和 webpack 导入模块时重构代码以避免 ciruclar 依赖
- google-cloud-platform - Google 管理员:无法重定向主域
- ios - 将可选图像 UIImageView 传递给另一个视图控制器