Похожие чаты

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 ответов

21 просмотр
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?

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

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

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
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
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта