Springboot~mongo Embedded Collection Operations

The various operations on the embedded objects of mongodb have already been mentioned in the .net platform. At the same time, the uncle has also encapsulated the mongo storage by himself, which is also very convenient to use. Of course, there are corresponding methods in the java springboot framework. The following main Let me talk about it, I hope it will be helpful to students who are new to mongodb!

A test data structure of DEMO

/**
 * address.
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Address {

  /**
   * serial number.
   */
  @Id
  private String id;
  /**
   * Province.
   */
  private String province;
  /**
   * city.
   */
  private String city;
  /**
   * Area.
   */
  private String district;
  /**
   * condition.
   */
  private Status status;
  /**
   * Extension.
   */
  private List<AddressExtension> addressExtension;
}

Among them, it has an embedded collection object, addressExtension, and we added some test data, as shown in the figure:

Here are some common data operations:

 /**
   * retrieve 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 inline document.
   *
   * @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 does nothing if the data already exists, and push will insert the same data 
    update.addToSet("addressExtension" , ext);
    mongoTemplate.upsert(query, update, Address.class);
  }

  /**
   * Update an element in the 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 in embedded document.
   *
   * @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, the update of the embedded collection is special, it uses the subscript of the collection element to update, $ is the subscript of the element that is currently to be updated!

Thanks for reading!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324777629&siteId=291194637