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 The filter query for the update is derived from the method name. The update is “as is” and does not bind any parameters.
2 The actual increment value is defined by the increment method argument that is bound to the ?1 placeholder.
3 Use the Spring Expression Language (SpEL) for parameter binding.
4 Use the pipeline attribute to issue aggregation pipeline updates.
5 The update may contain complex objects.
6 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.

Example 2. Delete…By Query
Imperative
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 Using a return type of List retrieves and returns all matching documents before actually deleting them.
2 A numeric return type directly removes the matching documents, returning the total number of documents removed.
3 A single domain type result retrieves and removes the first matching document.
4 Same as in 3 but wrapped in an Optional type.
Reactive
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
}
5 Using a return type of Flux retrieves and returns all matching documents before actually deleting them.
6 A numeric return type directly removes the matching documents, returning the total number of documents removed.
7 A single domain type result retrieves and removes the first matching document.