ReactiveMongoTemplate

https://blog.csdn.net/tianwenxue/article/details/120391734

常用注解

注解名称
作用对象
功能

@Document

实体类

类似于 hibernate 的 entity 注解,标明由 mongo 来维护该表

@CompoundIndex

实体类

复合索引

@Id

字段

和 mongoDB 中 _id 字段对应

@Field

字段

可以通过该注解自定义在 mongo 中字段的名称

@Indexed

字段

声明该字段需要加索引,加索引后以该字段为条件检索将大大提高速度

@Transient

字段

表示该字段不在 mongo 中存储,既忽略该字段

@Data
@Document(collection = "book")
public class Book {
    @Id
    private String id;
    private String name;
    private Double price;
    private String publisher;
    private Author[] authors;
    private String[] tags;
}

核心对象

Query

表示查询条件,可以通过 Query 对象来指定查询条件,如查询的字段、查询的条件、查询的排序等。

org.springframework.data.mongodb.core.query.Query

常用方法

public Query addCriteria(CriteriaDefinition criteriaDefinition);

public Query skip(long skip);

public Query limit(int limit);

public Query with(Pageable pageable);

public Query with(Sort sort);

Criteria

用于构建动态查询条件,可以轻松地将多个条件链接在一起

org.springframework.data.mongodb.core.query.Criteria

常用方法

public static Criteria where(String key);
public Criteria and(String key);
public Criteria is(@Nullable Object value);

Sort

用于构建排序条件,可以通过 Sort 对象来指定排序条件

org.springframework.data.domain.Sort

常用方法

public static Sort by(Sort.Order... orders);
public static Sort.Order order(Direction direction, String property);

Pageable

用于构建分页条件,可以通过 Pageable 对象来指定分页条件

org.springframework.data.domain.Pageable

常用方法

public static Pageable of(int page, int size);
public static Pageable of(int page, int size, Sort sort);

Demo: 查询所有书籍

Criteria criteria = Criteria.where("price").lt(50);  // 查询价格小于 50 的书籍
Query query = new Query(criteria);                 // 构建查询条件
query.with(Sort.by(Sort.Order.asc("price")));    // 按价格升序排序
query.skip(0).limit(2);                        // 分页查询,查询第一页的两条数据
List<Book> books = mongoTemplate.find(query, Book.class);

// Pageable pageable = PageRequest.of(0, 2, Sort.by(Sort.Order.asc("price")));
// query.with(pageable);

Demo: 查询指定字段

Criteria criteria = Criteria.where("price").lt(50).and("tags").in("编程");  // 查询价格小于 50 并且标签包含编程的书籍
Query query = new Query(criteria);                 // 构建查询条件
query.fields().include("name").include("price");  // 查询指定字段
List<Book> books = mongoTemplate.find(query, Book.class);

Update

表示更新条件,可以通过 Update 对象来指定更新条件,如更新的字段、更新的值等。

org.springframework.data.mongodb.core.query
// 更新字段
public static Update update(String key, @Nullable Object value);

public Update set(String key, @Nullable Object value);

// 设置字段值,字段不存在则新建字段
public Update setOnInsert(String key, @Nullable Object value);

// 删除字段
public Update unset(String key);

public void inc(String key);

// 字段值自增
public Update inc(String key, Number inc);

// 将 value 值追加到 key 数组中,如果 key 不存在,新建 key
public Update push(String key, @Nullable Object value);

// 将 value 添加到数组中,若数组中已存在则不添加
public Update addToSet(String key, @Nullable Object value);
public AddToSetBuilder addToSet(String key);

// 删除数组的第一(-1) 或者最后一个元素(1)
public Update pop(String key, Position pos);

// 数组中删除元素
public Update pull(String key, @Nullable Object value);
public Update pullAll(String key, Object[] values);

// 重命名字段
public Update rename(String oldName, String newName);
public Update currentDate(String key);
public Update currentTimestamp(String key);
public Update multiply(String key, Number multiplier);
public Update max(String key, Object value);
public Update min(String key, Object value);

最后更新于

这有帮助吗?