【发布时间】:2020-02-12 06:01:40
【问题描述】:
我想进行 upsert 调用来更新以及将我的数据插入到 mongo db 的嵌套数组中。
这是我的 mongo 文档。
{
"_id" : "575",
"_class" : "com.spyne.sharing.SpyneShareUserProject",
"spyneSharePhotoList" : [
{
"_id" : "fxLO68XyMR",
"spyneShareUsers" : [
{
"_id" : "chittaranjan@eventila.com",
"selectedByClient" : false
},
{
"_id" : "chittaranjan@gmail.com",
"selectedByClient" : false
}
]
},
{
"_id" : "nVpD0KoQAI",
"spyneShareUsers" : [
{
"_id" : "chittaranjan@eventila.com",
"selectedByClient" : true
}
]
},
{
"_id" : "Pm0B3Q9Igv",
"spyneShareUsers" : [
{
"_id" : "chittaranjan@gmail.com",
"selectedByClient" : true
}
]
}
]
}
这是我的要求,
- 假设我有一个 ID,即 575 (_id)
- 然后我将拥有嵌套数组 ID,即 fxLO68XyMR (spyneSharePhotoList._id)
- 然后我将嵌套电子邮件 ID 作为 ID,即 chittaranjan@eventila.com (spyneSharePhotoList.spyneShareUsers._id) 和 selectedByClient (boolean)
现在我想要检查此 ID (spyneSharePhotoList.spyneShareUsers._id) 是否已经存在于我想要更新布尔值的所需位置,即 selectedByClient (true / false)电子邮件 ID。
如果 id 不存在于数组中,它将创建一个新条目。作为
{
"_id" : "chittaranjan@gmail.com",
"selectedByClient" : false
}
在 spyneShareUsers 列表中。
请帮我完成这项任务。谢谢
【问题讨论】:
-
您可以发送您的 mongo 查询吗?
-
现在我只得到 spyneShareUsers 列表。通过聚合文档
-
@MrFreak : MongoDB 版本?
-
@whoami 版本 - 3.5
标签: arrays mongodb mongodb-query aggregate mongotemplate