As for the operation of mongodb's embedded objects, uncle has said in the. net platform. At the same time, uncle has encapsulated mongo's storage and is very convenient to use. In the java springboot framework, of course, there are corresponding methods. Let's talk about it. I hope it can help the students who just contacted mongodb!
Data structure of test DEMO
/** * Address. */ @Data @NoArgsConstructor @AllArgsConstructor public class Address { /** * Number. */ @Id private String id; /** * Province. */ private String province; /** * City. */ private String city; /** * District. */ private String district; /** * Status. */ private Status status; /** * Expand. */ private List<AddressExtension> addressExtension; }
There is an embedded collection object, addressExtension. We add some test data, as shown in the figure:
Here are some common data operations:
/** * Get data * * @param province province * @param city city * @return */ @Override public Address findByProvinceAndCity(String province, String city) { Query query = new Query(Criteria.where("province").is(province).and("city").is(city)); return mongoTemplate.findOne(query, Address.class, "address"); } /** * Update fields * * @param address . */ @Override public void updateCity(Address address) { Query query = new Query(Criteria.where("_id").is(address.getId())); Update update = Update.update("city", address.getCity()); mongoTemplate.upsert(query, update, Address.class); } /** * Add data from embedded documents * * @param id . * @param info . */ @Override public void addAddressInfo(String id, String info) { Query query = Query.query(Criteria.where("_id").is(id)); AddressExtension ext = new AddressExtension(); ext.setInfo(info); Update update = new Update();//update.push("Students", student); // addToSet If the data already exists, do nothing, and push Will insert the same data update.addToSet("addressExtension", ext); mongoTemplate.upsert(query, update, Address.class); } /** * Update an element in an embedded document * * @param oldInfo * @param newInfo */ @Override public void updateAddressInfo(String oldInfo, String newInfo) { Query query = new Query(Criteria.where("addressExtension.info").is(oldInfo)); Update update = new Update(); update.set("addressExtension.$.info", newInfo); mongoTemplate.upsert(query, update, Address.class); } /** * lambda filter. * @param list * @param predicate * @return */ public List<AddressExtension> conditionFilter(List<AddressExtension> list, Predicate<AddressExtension> predicate){ return list.stream().filter(predicate).collect(Collectors.toList()); } /** * Delete data from embedded documents * * @param id . * @param addressExtension . */ @Override public void delAddressInfo(String id, AddressExtension addressExtension) { Query query = Query.query(Criteria.where("_id").is(id)); Update update = new Update(); update.pull("addressExtension", addressExtension); mongoTemplate.updateFirst(query, update, Address.class); } /** * Delete the document * * @param id . */ @Override public void delAddress(String id) { Query query = Query.query(Criteria.where("_id").is(id)); mongoTemplate.remove(query, Address.class); }
Among them, updating the embedded set is very special. It uses the subscript of the set element to update. $is the subscript of the current element to be updated!
Thank you for reading!