gpt4 book ai didi

java - MongoDB 更新嵌套数组的元素

转载 作者:行者123 更新时间:2023-12-01 09:56:56 24 4
gpt4 key购买 nike

我有一个名为 Firma 的 mongo 集合,其文档结构之一如下:

 {
"_id" : ObjectId("5729af099b3ebf1d0ca7ff05"),
"musteriler" : [
{
"_id" : "de0bf813-b707-4a8d-afc2-9752e05c3aa5",
"yetkiliListesi" : [
{
"_id" : "a5e487fa-2034-4817-94f2-3bd837b76284",
"ad" : "Burak",
"soyad" : "Duman 1",
"cepTel" : "3333333333333",
"mail" : "asdf@asdf.com"
},
{
"_class" : "com.bisoft.entity.MusteriYetkili",
"_id" : "bc4b537d-522a-4c9a-9f67-8ca243e18f46",
"ad" : "Ridvan",
"soyad" : "ENİŞ",
"cepTel" : "222222222222",
"mail" : "asdf@asdf.com"
}
]
}
],
"defaultTimezone" : "Europe/Istanbul"
}

在上面的json中,我需要更新第二个数组(yetkiliListesi)的元素,其中_id =“a5e487fa-2034-4817-94f2-3bd837b76284”

因为我使用java应用程序(使用mongo java驱动程序和spring boot MongoTemplate)来访问它并执行此查询:

 mongoTemplate.updateFirst(Query.query(Criteria.where("_id").is("5729af099b3ebf1d0ca7ff05").and("musteriler.yetkiliListesi._id").is("a5e487fa-2034-4817-94f2-3bd837b76284")), 
new Update().set("musteriler.yetkiliListesi.$", yetkiliDBO), Firma.class);

在上面的查询中,yetkiliDBO 是一个 BasicDBObject 及其内容:

yetkiliDBO = { 
'_class': 'com.bisoft.entity.MusteriYetkili',
'_id': "a5e487fa-2034-4817-94f2-3bd837b76284",
'ad': 'wer',
'soyad': 'xyz',
'cepTel': "222222222222",
mail: "asdf@asdf.com"
}

执行查询时出现错误

com.mongodb.WriteConcernException: { "serverUsed" : "192.168.2.250:27017" , "ok" : 1 , "n" : 0 , "updatedExisting" : false , "err" : "cannot use the part (musteriler of musteriler.yetkiliListesi.0) to traverse the element 

我需要做什么?

最佳答案

遍历嵌套数组时不能使用“$”占位符。

The positional $ operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the $ placeholder is a single value

source

我建议将您的数据重组为单独的、嵌套较少的集合。

关于java - MongoDB 更新嵌套数组的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37141343/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com