Spring DATA MongoDB @DBref查询,or和and联合查询

时间:2021-04-23 00:47:49

@DBref文档关联,在按该类型查询的时候,在字段名后加上关联表的字段名即可,如:

Criteria.where("bloggroup.$id"),

$id代表关联表的oid字段。

 

or和and联合查询

比如查询 (A = 1 and b = 2 )or (A = 3 and b =4),or的数量不确定,

那么采用定义一个Criteria数组,将条件组记入数组中,然后将数组指定给orCriteria方法。

@Override
public List<Blog> find(List<GroupAttention> groupattentions) {
  Query query = new Query();
  Criteria criteria = new Criteria();
  Criteria[] criterialist = new Criteria[groupattentions.size()];
  for(int i=0;i<groupattentions.size();i++){
    Criteria criteriaand = new Criteria();
    criteriaand.andOperator(Criteria.where("bloggroup.$id").is(new ObjectId(groupattentions.get(i).getGroupid())).and("bloglevel").gte(groupattentions.get(i).getGrouplevel()));
    criterialist[i] = criteriaand;
  }
  criteria.orOperator(criterialist);
  query.addCriteria(criteria);
  return mongoTemplateDataBase.find(query, Blog.class);
}