MongoDB-specific Data Manipulation Methods

除了 query methods 之外,还可以使用专门的方法更新数据。

Next to the query methods it is possible to update data with specialized methods.

Update Methods

你还可以使用前表中的关键字来创建查询,这些查询标识与之匹配的文档以对它们运行更新。实际更新操作是由方法本身上的`@Update`注释定义的,如下面的清单所示。请注意,派生查询的命名模式以`find`开头。只有与`@Query`结合使用才允许使用`update`(如`updateAllByLastname(…​)`)。

You can also use the keywords in the preceding table to create queries that identify matching documents for running updates on them. The actual update action is defined by the @Update annotation on the method itself, as the following listing shows. Note that the naming schema for derived queries starts with find. Using update (as in updateAllByLastname(…​)) is allowed only in combination with @Query.

更新应用于*所有*匹配的文档,并且*不能*通过传入`Page`或使用任何[限制关键字,repositories.limit-query-result]来限制范围。返回类型可以是`void`或数字类型,例如`long`,以保存已修改文档的数量。

The update is applied to all matching documents and it is not possible to limit the scope by passing in a Page or by using any of the repositories.limit-query-result. The return type can be either void or a numeric type, such as long, to hold the number of modified documents.

Example 1. Update Methods
public interface PersonRepository extends CrudRepository<Person, String> {

    @Update("{ '$inc' : { 'visits' : 1 } }")
    long findAndIncrementVisitsByLastname(String lastname); 1

    @Update("{ '$inc' : { 'visits' : ?1 } }")
    void findAndIncrementVisitsByLastname(String lastname, int increment); 2

    @Update("{ '$inc' : { 'visits' : ?#{[1]} } }")
    long findAndIncrementVisitsUsingSpELByLastname(String lastname, int increment); 3

    @Update(pipeline = {"{ '$set' : { 'visits' : { '$add' : [ '$visits', ?1 ] } } }"})
    void findAndIncrementVisitsViaPipelineByLastname(String lastname, int increment); 4

    @Update("{ '$push' : { 'shippingAddresses' : ?1 } }")
    long findAndPushShippingAddressByEmail(String email, Address address); 5

    @Query("{ 'lastname' : ?0 }")
    @Update("{ '$inc' : { 'visits' : ?1 } }")
    void updateAllByLastname(String lastname, int increment); 6
}
1 用于更新的过滤器查询是从方法名称派生的。更新是 “as is”,并且不绑定任何参数。
2 The filter query for the update is derived from the method name. The update is “as is” and does not bind any parameters.
3 实际增量值由绑定到 ?1 占位符的 increment 方法参数定义。
4 The actual increment value is defined by the increment method argument that is bound to the ?1 placeholder.
5 使用 Spring 表达式语言 (SpEL) 进行参数绑定。
6 Use the Spring Expression Language (SpEL) for parameter binding.
7 使用 pipeline 属性发送 aggregation pipeline updates
8 Use the pipeline attribute to issue aggregation pipeline updates.
9 更新可能包含复杂对象。
10 The update may contain complex objects.
11 string based query 与更新相结合。
12 Combine a string based query with an update.

存储库更新不会发出持久性或映射生命周期事件。

Repository updates do not emit persistence nor mapping lifecycle events.

Delete Methods

前表中的关键字可以与`delete…By`或`remove…By`结合使用,以创建删除匹配文档的查询。

The keywords in the preceding table can be used in conjunction with delete…By or remove…By to create queries that delete matching documents.

Delete…By Query
  • Imperative

  • Reactive

public interface PersonRepository extends MongoRepository<Person, String> {

    List <Person> deleteByLastname(String lastname);      1

    Long deletePersonByLastname(String lastname);         2

    @Nullable
    Person deleteSingleByLastname(String lastname);       3

    Optional<Person> deleteByBirthdate(Date birthdate);   4
}
1 使用返回类型 List 会在实际删除所有匹配文档之前检索并返回它们。
2 Using a return type of List retrieves and returns all matching documents before actually deleting them.
3 数值返回类型直接删除匹配文档,返回已删除文档的总数。
4 A numeric return type directly removes the matching documents, returning the total number of documents removed.
5 单一域类型结果检索并删除第一个匹配文档。
6 A single domain type result retrieves and removes the first matching document.
7 与第 3 步相同,但用 Optional 类型包装。
8 Same as in 3 but wrapped in an Optional type.
public interface PersonRepository extends ReactiveMongoRepository<Person, String> {

    Flux<Person> deleteByLastname(String lastname);      1

    Mono<Long> deletePersonByLastname(String lastname);         2

    Mono<Person> deleteSingleByLastname(String lastname);       3
}
1 使用返回类型 Flux 会在实际删除所有匹配文档之前检索并返回它们。
2 Using a return type of Flux retrieves and returns all matching documents before actually deleting them.
3 数值返回类型直接删除匹配文档,返回已删除文档的总数。
4 A numeric return type directly removes the matching documents, returning the total number of documents removed.
5 单一域类型结果检索并删除第一个匹配文档。
6 A single domain type result retrieves and removes the first matching document.