针对mongotemplate的查询写法,可能会跟其他orm框架里面的查询写法混淆,特地记录一下.
一. 常用查询:
public PageUrl getByUrl(String url) {
return findOne(new Query(Criteria.where("url").is(url)),PageUrl.class);
}
public List getPageUrlsByUrl(int begin, int end,String linkUrlid) {
Query query = new Query();
query.addCriteria(Criteria.where("linkUrl.id").is(linkUrlid));
return find(query.limit(end - begin).skip(begin), PageUrl.class);
}
public long getProcessLandLogsCount(List conditions)
{
Query query = new Query();
if (conditions != null && conditions.size() > 0) {
for (Condition condition : conditions) {
query.addCriteria(Criteria.where(condition.getKey()).regex(".*?\\" +condition.getValue().toString()+ ".*"));
}
}
return count(query, ProcessLandLog.class);
}
最下面,我在代码亲自实践过的模糊查询,只支持字段属性是字符串的查询,你要是查字段属性是int的模糊查询,还真没辙。
public List<ProcessLandLog> getProcessLandLogs(int begin,int end,List<Condition> conditions,String orderField,Direction direction)
{
Query query = new Query();
if (conditions != null && conditions.size() > 0) {
for (Condition condition : conditions) {
if(condition.getKey().equals("time")){
query.addCriteria(Criteria.where("time").gte(condition.getValue())); //gte: 大于等于
}else if(condition.getKey().equals("insertTime")){
query.addCriteria(Criteria.where("insertTime").gte(condition.getValue()));
}else{
query.addCriteria(Criteria.where(condition.getKey()).is(condition.getValue()));
}
}
}
return find(query.limit(end - begin).skip(begin).with(new Sort(new Sort.Order(direction, orderField))), ProcessLandLog.class);
}
public List<DpsLand> getDpsLandsByTime(int begin, int end, Date beginDate,Date endDate) {
return find(new Query(Criteria.where("updateTime").gte(beginDate).lte(endDate)).limit(end - begin).skip(begin),
DpsLand.class);
}
查询字段不存在的数据
public List getGoodsDetails2(int begin, int end) {
Query query = new Query();
query.addCriteria(Criteria.where("goodsSummary").not());
return find(query.limit(end - begin).skip(begin),GoodsDetail.class);
}
查询字段不为空的数据
Criteria.where("key1").ne("").ne(null)
查询或语句:a || b
Criteria criteria = new Criteria();
criteria.orOperator(Criteria.where("key1").is("0"),Criteria.where("key1").is(null));
查询且语句:a && b这里写代码片
Criteria criteria = new Criteria();
criteria.and(“key1”).is(false);
criteria.and(“key2”).is(type);
Query query = new Query(criteria);
long totalCount = this.mongoTemplate.count(query, Xxx.class);
查询一个属性的子属性,例如:查下面数据的key2.keyA的语句
var s = {
key1: value1,
key2: {
keyA: valueA,
keyB: valueB
}
};
@Query("{'key2.keyA':?0}")
List<Asset> findAllBykeyA(String keyA);
public long getPageInfosCount(List conditions) {
Query query = new Query();
if (conditions != null && conditions.size() > 0) {
for (Condition condition : conditions) {
query.addCriteria(Criteria.where(condition.getKey()).is(condition.getValue()));
}
}
return count(query, PageInfo.class);
}
public WriteResult updateTime(PageUrl pageUrl) {
String id = pageUrl.getId();
return updateFirst(new Query(Criteria.where("id").is(id)),Update.update("updateTime", pageUrl.getUpdateTime()), PageUrl.class);
}
//调用更新
private void updateProcessLandLog(ProcessLandLog processLandLog,
int crawlResult) {
List fields = new ArrayList();
List
public void deleteObject(Class clazz,String id) {
remove(new Query(Criteria.where("id").is(id)),clazz);
}
9.保存数据:
//插入一条数据
public void saveObject(Object obj) {
insert(obj);
}
//插入多条数据
public void saveObjects(List objects) {
for(T t:objects){
insert(t);
}
}