Похожие чаты

Hi, could you please help me to find an answer

for this question?

https://stackoverflow.com/questions/72055175/ways-to-update-many-nested-documents-with-mongotemplate-without-creating-additio

4 ответов

18 просмотров
Ulvi- Автор вопроса

Hi. I use MongoTemplate to update a document using findAndModify() method. There are nested documents inside one document. Example as following: @Document public class Company { private Long id; private String postalCode; private int floorCount; ..... private Set<Car> cars; } class Car { private Long id; private int seatCount; private String color; ..... private Engine engine; } class Engine { private Long id; private float engineVolume; ..... } In order to send to findAndModify() method as a parameter, I created an Update object for a Company as following: Update update = new Update(); update.set("postalCode", company.getPostalCode()); update.set("floorCount", company.getFloorCount()); .... There are many Cars for a Company and update requests of Company comes with Car and Engine details. To update Cars of Company, for nested Car documents, I write another separate findAndModify() method as following: companyRequest.getCars().forEach( car -> { mongoTemplate.findAndModify( new Query(where("car._id").is(car.getId())), new Update() .set("car.$[].seatCount", car.getSeatCount()) .set("car.$[].color", car.getColor()), Company.class ); } ); What I want to do is I would like to build only one Update object for entire Company document with Car and Engine and set new values to only one Update object without iterating over Cars and without using another separate findAndModify() method for Car and use only one findAndModify(), requirement is to send only one request to MongoDB. How can I do?

Ulvi
Hi. I use MongoTemplate to update a document using...

What exact fields are you trying to update in "cars" sub-documents? Your code as it is right now, doesn't seem to effectively change anything. I mean, it feels like the effect is car[0].seatCount = car[0].seatCount, then next car[1].seatCount = car[1].seatCount and so on. Which means that you're setting values in db to the same values they were before. Or am I missing something? Can you please clarify what is the business goal of the query you're trying to compose? E.g. - "I want to update postal code of company and paint all cars to green in a single query". Something like that

Ulvi- Автор вопроса
Dmytro Buryak
What exact fields are you trying to update in "car...

companyRequest is the request coming from front end. And as I show, I want to update the fields of Car class and Company fields with only one query to MongoDB . Goal is written at the end of the question. What else would you like to know about question?

Ulvi
companyRequest is the request coming from front en...

Ah, ok, got it. Now companyRequest makes sense to me. Still this part is not clear for me though: update.set("postalCode", company.getPostalCode()); update.set("floorCount", company.getFloorCount()); new values for postal code and floor count are fetched from company object, not companyRequest, right? Where that company object comes from?

Похожие вопросы

Обсуждают сегодня

This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
есть ПО, которое лежит папкой, по сути и не инсталится, а просто запускается. Надо раскидать по машинам в домене. Я так понял, что прям настройками GPO, копировать я смогу тол...
Dmitriy
8
any reference of this implementation?
BitBuddha
29
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
Привет)) уже кажется эту тему перемусолили, но вот я так и не понял. Я сейчас сижу на 27дюймов 2к мониторе. На Актуальной макоси, если я куплю 27д 4к монитор: - будет ли изобр...
Vladislav Piskunov
16
Добрый день! Скажите пожалуйста, а какие программы вы бы рекомендовали написать для того, чтобы научиться управлять памятью? Можно написать динамический массив, можно связный ...
Филипп
7
Also, why can’t the community have a vote/ say when it comes to initiatives like buybacks. Isn’t the point of crypto decentralisation? Don’t we deserve input as long term supp...
👨🏽‍🦰
13
Карта сайта