How to embed values ​​in internal arraylist when updating external arraylist in mongodb?

I am working with Spring MongoDB, and now I am faced with the problem of inserting values ​​into arraylist. Here is my POJO class class ...

public class Search implements Serializable { private static final long serialVersionUID = 1L; @Id private String id; private String searchkey; private ArrayList<Lead> leads; } 

Lead is another POJO class that looks like ...

 public class Lead implements Serializable { private static final long serialVersionUID = 1L; private String leadtext; private String address; private ArrayList<History> trackrecords; } 

"History" is a POJO class that is similar to.

 public class History implements Serializable { private static final long serialVersionUID = 1L; private String id; private String changedfield; private String oldvalue; private String newvalue; } 

and the problem is that I want to insert data into the tracks while updating one of them. is this possible in Spring mongotemplate .. ?? if possible then please help me. Thank you in advance

+5
source share
2 answers

Please try this.

Suppose leadtext can find this lead element uniquely.

 Query query = new Query().addCriteria(Query.where("searchkey").is(searchkey).and("leads.leadtext").is(leadtext)); Update update = new Update().push("leads.$.trackrecords", trackrecord); mongoTemplate.updateFirst(query, update, Search.class); 
+4
source

use mongodb $ push to insert or update an existing arraylist

+2
source

Source: https://habr.com/ru/post/1202950/


All Articles