@Data
public class LoginLog implements Serializable {
private static final long serialVersionUID = -4914360977314126743L;
@Id
private String id;
/**
* 用户名称
*/
private String username;
/**
* 用户类型
*/
private Integer userType;
/**
* 登录时间
*/
private LocalDateTime loginTime;
}
表示条件、投影、排序和查询提示的 MongoDB 查询对象
org.springframework.data.mongodb.core.query.Query
构造方法
public Query(CriteriaDefinition criteriaDefinition) {
addCriteria(criteriaDefinition);
}
常用方法
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)
用于创建查询的中心类。 它遵循流畅的 API 风格,可以轻松地将多个条件链接在一起
org.springframework.data.mongodb.core.query.Criteria
构造方法
public Criteria() {
this.criteriaChain = new ArrayList<Criteria>();
}
public Criteria(String key) {
this.criteriaChain = new ArrayList<Criteria>();
this.criteriaChain.add(this);
this.key = key;
}
protected Criteria(List<Criteria> criteriaChain, String key) {
this.criteriaChain = criteriaChain;
this.criteriaChain.add(this);
this.key = key;
}
常用方法
public static Criteria where(String key)
public Criteria and(String key)
public Criteria is(@Nullable Object value)
用于轻松构建 MongoDB 更新子句的类
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)
public <T> T save(T objectToSave)
public <T> T save(T objectToSave, String collectionName)
public <T> T insert(T objectToSave)
public <T> T insert(T objectToSave, String collectionName)
public <T> Collection<T> insertAll(Collection<? extends T> objectsToSave)
public DeleteResult remove(Object object)
public DeleteResult remove(Object object, String collectionName)
public DeleteResult remove(Query query, String collectionName)
public DeleteResult remove(Query query, Class<?> entityClass)
底层都是调用 doRemove
protected <T> DeleteResult doRemove(String collectionName, Query query, @Nullable Class<T> entityClass,
boolean multi)
public UpdateResult updateFirst(Query query, UpdateDefinition update, Class<?> entityClass)
public UpdateResult updateFirst(Query query, UpdateDefinition update, String collectionName)
public UpdateResult updateFirst(Query query, UpdateDefinition update, Class<?> entityClass, String collectionName)
public UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass)
public UpdateResult updateMulti(Query query, UpdateDefinition update, String collectionName
public UpdateResult updateMulti(Query query, UpdateDefinition update, Class<?> entityClass, String collectionName)
示例: update 用于设置需要更新哪些字段
Query query = new Query();
Criteria criteria = new Criteria();
criteria.and("username").is("upt1");
query.addCriteria(criteria);
Update update = new Update();
update.set("username", "up");
//单条
mongoTemplate.updateFirst(query, update, LoginLog.class);
//所有
mongoTemplate.updateMulti(query, update, LoginLog.class);
public <T> T findOne(Query query, Class<T> entityClass)
public <T> T findOne(Query query, Class<T> entityClass, String collectionName)
public <T> List<T> find(Query query, Class<T> entityClass)
public <T> List<T> find(Query query, Class<T> entityClass, String collectionName)
查一个集合所有数据
public <T> List<T> findAll(Class<T> entityClass)
public <T> List<T> findAll(Class<T> entityClass, String collectionName)
public <T> T findAndModify(Query query, UpdateDefinition update, Class<T> entityClass)
public <T> T findAndModify(Query query, UpdateDefinition update, Class<T> entityClass, String collectionName)
public <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions options, Class<T> entityClass)
public <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions options, Class<T> entityClass,String collectionName)
只会修改 指定条件下 Update 指定的 key 的值
public <T> T findAndModify(Query query, UpdateDefinition update, Class<T> entityClass)
public <T> T findAndModify(Query query, UpdateDefinition update, Class<T> entityClass, String collectionName)
public <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions options, Class<T> entityClass)
public <T> T findAndModify(Query query, UpdateDefinition update, FindAndModifyOptions options, Class<T> entityClass,
String collectionName)
将符合条件的数据替换成 新的对象的值 id 不会重新生成
public <T> T findAndRemove(Query query, Class<T> entityClass)
public <T> T findAndRemove(Query query, Class<T> entityClass, String collectionName)
如果有多个数据符合条件只会返回一个对象,并在 mongo 中删除
确定给定查询的结果至少包含一个元素
public boolean exists(Query query, Class<?> entityClass)
public boolean exists(Query query, String collectionName)
public boolean exists(Query query, @Nullable Class<?> entityClass, String collectionName)
public long count(Query query, Class<?> entityClass)
public long count(Query query, String collectionName)
public long count(Query query, @Nullable Class<?> entityClass, String collectionName)
public <T> MapReduceResults<T> mapReduce(String inputCollectionName, String mapFunction, String reduceFunction,
Class<T> entityClass)
聚合管道作为替代方案
从MongoDB 5.0开始,不推荐使用map reduce操作。
聚合管道提供了比map-reduce操作更好的性能和可用性。
可以使用聚合管道操作符(如 g r o u p 、 group、 group、merge等)重写Map reduce操作。
对于需要自定义功能的map reduce操作,MongoDB从4.4版开始提供 a c c u m u l a t o r 和 accumulator和 accumulator和function聚合运算符。
https://docs.mongodb.com/manual/core/map-reduce/
public <T> GroupByResults<T> group(String inputCollectionName, GroupBy groupBy, Class<T> entityClass)
public <T> GroupByResults<T> group(@Nullable Criteria criteria, String inputCollectionName, GroupBy groupBy,
Class<T> entityClass)
public <O> AggregationResults<O> aggregate(TypedAggregation<?> aggregation, Class<O> outputType)
public <O> AggregationResults<O> aggregate(TypedAggregation<?> aggregation, String inputCollectionName,
Class<O> outputType)
public <O> AggregationResults<O> aggregate(Aggregation aggregation, Class<?> inputType, Class<O> outputType)
public <O> AggregationResults<O> aggregate(Aggregation aggregation, String collectionName, Class<O> outputType)
public <O> CloseableIterator<O> aggregateStream(TypedAggregation<?> aggregation, String inputCollectionName,
Class<O> outputType)
public <O> CloseableIterator<O> aggregateStream(TypedAggregation<?> aggregation, Class<O> outputType)
public <O> CloseableIterator<O> aggregateStream(Aggregation aggregation, Class<?> inputType, Class<O> outputType)
public <O> CloseableIterator<O> aggregateStream(Aggregation aggregation, String collectionName, Class<O> outputType)