Skip to content
广告❤️成为赞助商

管道函数构造器

使用MongoPlus快速构建管道条件

说明:

  • 2.1.2+版本已删除
  • 下列管道函数中,参数为BasicDBObject和Bson的,为自定义条件,可以自己构建管道参数,包括custom方法

match

java
match(QueryChainWrapper<T, ?> queryChainWrapper);
match(BasicDBObject basicDBObject);
match(Bson bson);
  • 过滤文档记录,只将匹配的文档记录传递到管道中的下一个步骤
  • 例: match(new Wrapper<>().eq(User::getUserName,"张三"))

sort

java
sort(Order... orders);
sort(List<Order> orderList);
sortAsc(SFunction<T,Object>... field);  //正序排序
sortAsc(String... field); //正序排序
sortDesc(SFunction<T,Object>... field); //倒序排序
sortDesc(String... field);  //倒序排序
sort(BasicDBObject basicDBObject);
sort(Bson bson);
  • 对所有输出的文档记录进行排序
  • 例: sort(new Order(1,"createTime"));

limit

java
limit(long limit);
  • 限制管道中文档记录的数量
  • 例: limit(10)

skip

java
skip(long skip);
  • 跳过指定数量的文档记录,返回剩下的文档记录(当前页)
  • 例: skip(1)

group

java
group(SFunction<T,Object> _id);
group(String _id);
group(List<GroupField> _id);
group(Accumulator... _id);
group(SFunction<T,Object> _id,Accumulator accumulator);
group(String _id,Accumulator accumulator);
group(SFunction<T,Object> _id , Accumulator... accumulator);
group(String _id , Accumulator... accumulator);
group(SFunction<T,Object> _id , List<Accumulator> accumulatorList);
group(String _id , List<Accumulator> accumulatorList);
group(SFunction<T,Object> _id , String resultMappingField,String operator,String field);
group(String _id , String resultMappingField,String operator,String field);
group(SFunction<T,Object> _id , String resultMappingField, GroupTypeEnum operator, String field);
group(String _id , String resultMappingField, GroupTypeEnum operator, String field);
group(SFunction<T,Object> _id , SFunction<T,Object> resultMappingField, String operator, SFunction<T,Object> field);
group(String _id , SFunction<T,Object> resultMappingField, String operator, SFunction<T,Object> field);
group(String _id , SFunction<T,Object> resultMappingField, GroupTypeEnum operator, SFunction<T,Object> field);
group(SFunction<T,Object> _id , SFunction<T,Object> resultMappingField, GroupTypeEnum operator, SFunction<T,Object> field);
group(BasicDBObject basicDBObject);
group(Bson bson);
  • 对所有文档记录进行分组,然后计算聚合结果
  • 方法中的Accumulator对象,是累加器的条件类,可以使用com.anwen.mongo.conditions.accumulator.AccumulatorInterface构建累加器,提供了first,last,sum,avg等等方法;
  • 例: group(User::getName,new AccumulatorInterface<>().sum(User::getAge))

lookup

java
lookup(String from,String localField,String foreignField,String as);
lookup(String from, List<Let> letList, AggregateChainWrapper<T,?> pipeline, String as);
lookup(BasicDBObject basicDBObject);
lookup(Bson bson);
  • 实现集合之间的join操作
  • 例: lookup("test","roleId","id","roleName")

addFields

java
addFields(String resultMappingField,SFunction<T,Object> field);
addFields(SFunction<T,Object> resultMappingField,SFunction<T,Object> field);
addFields(SFunction<T,Object> resultMappingField,String field);
addFields(String resultMappingField,String field);
addFields(AddFields... addFields);
addFields(List<AddFields> addFieldsList);
addFields(BasicDBObject basicDBObject);
addFields(Bson bson);
  • 向集合中添加新字段,可使用AddFields对象进行构建
  • 例: addFields("roleName","roleId")

unwind

java
unwind(SFunction<T, Object> field);
unwind(Boolean preserveNullAndEmptyArrays , SFunction<T, Object> field);
unwind(String field);
unwind(Boolean preserveNullAndEmptyArrays,String field);
unwind(BasicDBObject basicDBObject);
unwind(Bson bson);
  • 展开数组字段,生成一个文档副本,每个副本只包含一个数组元素
  • preserveNullAndEmptyArrays 是否保留空数组或包含 null 值的数组字段
  • 例: unwind(User::getRoleList)

sample

java
sample(long size);
  • 随机选择指定数量的文档
  • 例: sample(1)

replaceRoot

java
replaceRoot(SFunction<T,Object>... field);
replaceRoot(ReplaceRoot... replaceRoot);
replaceRoot(List<ReplaceRoot> replaceRootList);
replaceRoot(String... field);
replaceRoot(Boolean reserveOriginalDocument,SFunction<T,Object>... field);
replaceRoot(Boolean reserveOriginalDocument,String... field);
replaceRoot(BasicDBObject basicDBObject);
replaceRoot(Bson bson);
  • 使用指定字段替换根文档
  • 例: replaceRoot(User::UserName,User::getUserRole)

unionWith

java
unionWith(String collectionName);
unionWith(BasicDBObject basicDBObject);
unionWith(Bson bson);
  • unionAll
  • 例: unionWith("t_user")

out

java
out(String db,String coll);
out(BasicDBObject basicDBObject);
out(Bson bson);
  • 将管道中的文档记录输出到一个具体的集合中,不存在则自动创建
  • 这个必须是管道操作中的最后一步,如果输出到现有集合,会覆盖原数据
  • 例: out("mongoplus","user")

custom

java
custom(BasicDBObject basicDBObject);
custom(Bson bson);
  • 自定义管道操作,您可以随意的定制自己的管道
  • 例: custom(new BasicDBObject("$out",new BasicDBObject("role")));

链式调用 lambda 式

java
UserService userService;

List<User> userList = userService.lambdaAggregate().match().....list();