如何更新具有多层数组嵌套的文档中的记录?
我的文档结构如下:
semi.show
使用以下查询,向我返回上述文档。
[error] /Users/rajkumar.natarajan/Documents/Coding/kafka_demo/circe-demo/src/main/scala/ciris/config/loader/Config.scala:32:5: ambiguous implicit values:
[error] both value emptyProductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.HNil]
[error] and method emptyCoproductDerivedShow in trait MkShowDerivation of type => cats.derived.MkShow[shapeless.CNil]
[error] match expected type cats.derived.MkShow[A]
[error] show
[error] ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error]
我想用名词2更新参与者的公司领域。
答案 0 :(得分:1)
从Mongo 3.6开始,您可以通过合并以下运算符来update multi-nested arrays:
$set
(用于更新特定字段)$[]
(以匹配数组中的任何项目)$[<identifier>]
(以匹配数组中的特定项目)在这里,您可以更新具有proyectos
数组的特定reuniones
文档,该文档具有participantes
数组,该数组包含对象的字段nomina
等于{{ 1}}:
2
如果您想将查询限制为特定的// update a specific proyectos document
// that has a field "reuniones" which is an array
// in which each item is an object with a field "participantes" that is an array
// in which each item is an object that has a field "nomina" equal to 2
db.proyectos.update({
_id: ObjectId("5bfa09f0a0441f38d45dcc9c"),
}, {
$set: {
"reuniones.$[].participantes.$[j].firma": <your update>
},
}, {
arrayFilters: [
{
"j.nomina": 2
}
]
})
,则可以执行以下操作:
reunion
要更新所有满足以上条件的db.proyectos.update({
_id: ObjectId("5bfa09f0a0441f38d45dcc9c"),
}, {
$set: {
"reuniones.$[i].participantes.$[j].firma": <your update>
},
}, {
arrayFilters: [
{
"i._id": ObjectId("5bfa09f0a0441f38d45dcc99")
},
{
"j.nomina": 2
}
]
})
,只需省略proyectos
查询:
_id