0

I have a mongo collection named firma which has one of the document structure as below:

 {
    "_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" : "[email protected]"
                }, 
                {
                    "_class" : "com.bisoft.entity.MusteriYetkili",
                    "_id" : "bc4b537d-522a-4c9a-9f67-8ca243e18f46",
                    "ad" : "Ridvan",
                    "soyad" : "ENİŞ",
                    "cepTel" : "222222222222",
                    "mail" : "[email protected]"
                }
            ]
        }
    ],
    "defaultTimezone" : "Europe/Istanbul"
    }

In the above json, I need to update element of second array(yetkiliListesi) which _id = "a5e487fa-2034-4817-94f2-3bd837b76284"

Since I am using a java application(using mongo java driver and spring boot MongoTemplate) to access it and execute this query :

 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);

In the above query, yetkiliDBO is a BasicDBObject and its content :

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

when execute my query I have an error

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 

What I need to do?

1 Answer 1

1

You can not use the '$' placeholder when traversing nested arrays.

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

I would suggest restructuring your data into separate, less-nested collections.

Sign up to request clarification or add additional context in comments.

1 Comment

You could update an entire 'yetkiliListesi' array: new Update().set("musteriler.$.yetkiliListesi", List<yetkiliDBO>) but I would highly suggest restructuring your data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.