本文主要是介绍MongoDB中关于Criteria的各种查询实例(is、in、regex、gte、lte、elemMatch),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
例如:实体中有一集合,包含三个元素,A、B、C,实现至少含有其中一个元素就可以查询到此实体的时候就需要使用elemMatch来进行查询
package cn.com.qiyitech.qiyibiz.model.item;import cn.com.qiyitech.pfw.entity.PlatformAsset;
import cn.com.qiyitech.pfw.model.PlatformBaseFilter;
import cn.com.qiyitech.qiyibiz.entity.Item;
import cn.com.qiyitech.qiyibiz.entity.ItemFabric;
import cn.com.qiyitech.qiyibiz.entity.ItemOrder;
import cn.com.qiyitech.qiyibiz.entity.Shop;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.mongodb.core.query.Criteria;import java.util.Set;
@Setter
@Getter
public class ItemListFilter extends PlatformBaseFilter {@ApiModelProperty(value = "商品名称")private String name;@ApiModelProperty(value = "所属店铺ID")private Set<String> owner;@ApiModelProperty(value = "幅宽最大值")private Integer maxWidthCloth;@ApiModelProperty(value = "幅宽最小值")private Integer minWidthCloth;@ApiModelProperty(value = "克重单位")private String weightUnit;@ApiModelProperty(value = "产品规格")private String[] specification;@Overridepublic Criteria genCriteria() {Criteria criteria = new Criteria();if (specification != null && specification.length > 0)criteria = criteria.and(Item.FIELD_SPECIFICATION).elemMatch(new Criteria().in(specification));if (owner != null) criteria = criteria.and(PlatformAsset.FIELD_OWNER).in(owner);if (name != null)criteria = criteria.and(PlatformAsset.FIELD_NAME).regex(".*" + name + ".*");if (minWidthCloth != null || maxWidthCloth != null) {if (minWidthCloth == null) {minWidthCloth = 0;}if (maxWidthCloth == null) {maxWidthCloth = Integer.MAX_VALUE;}criteria = criteria.and(Item.FIELD_WIDTH_CLOTH).gte(minWidthCloth).lte(maxWidthCloth);}if (weightUnit != null) criteria = criteria.and(Item.FIELD_WEIGHT_UNIT).is(weightUnit);return criteria;}
}
这篇关于MongoDB中关于Criteria的各种查询实例(is、in、regex、gte、lte、elemMatch)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!