赞
踩
sort默认排序是使用字符串首字符大小排序
导致 2 会排在12前面
聚合排序时要使用Collection 设置转为数值来比较大小
Collation collation = Collation.of(Locale.CHINESE).numericOrdering(true);
query.collation(collation);
Query query = new Query();
Collation collation = Collation.of(Locale.CHINESE).numericOrdering(true);
if(pd.getString("orderNum").isEmpty() || pd.getString("orderNum") == ""){
query = new Query(Criteria.where("subjectType").is(pd.getString("subjectType")).and("openId").is(pd.getString("openId")).and("subject").is(pd.getString("subject"))).with(Sort.by(new Sort.Order(Sort.Direction.ASC, "OrderNum")));
query.collation(collation);
}else{
query = new Query(Criteria.where("subjectType").is(pd.getString("subjectType")).and("OrderNum").is(pd.getString("orderNum")).and("openId").is(pd.getString("openId")).and("subject").is(pd.getString("subject"))).with(Sort.by(new Sort.Order(Sort.Direction.ASC, "OrderNum"))).limit(1);
}
List<Map> list = mongoDBClient.selectByQuery("userAnsweRecords",query,Map.class);
最近在项目中使用了MongoDB,遇到一个问题是如何查询返回指定的字段
百度和谷歌中文搜索了各种答案没一个靠谱的
就说说百度上流传最广的一个互相抄袭的博客内容是这么写的
QueryBuilder queryBuilder = new QueryBuilder();
queryBuilder.or(new BasicDBObject("onumber", "002"), new BasicDBObject("cname","zcy1"));
BasicDBObject fieldsObject=new BasicDBObject();
fieldsObject.put("onumber", 1);
fieldsObject.put("cname", 1);
Query query=new BasicQuery(queryBuilder.get(),fieldsObject);
这个答案显然已经不合时宜,现在BasicQuery已经不能这样构造了。
最后还是在StackOverflow搜到了准确答案。
Query query = new Query();
query.fields().include("path"); //包含该字段
query.fields().exclude("salary");//不包含该字段
使用skip()
Query query = new Query();
Collation collation = Collation.of(Locale.CHINESE).numericOrdering(true);
query = new Query(Criteria.where("isCorrect").is("2").and("openId").is(pd.getString("openId")).and("subject").is(pd.getString("subject"))).with(Sort.by(new Sort.Order(Sort.Direction.ASC, "createDate"))).limit(1).skip(questionNum);
List<PageData> question = mongoDBClient.selectByQuery("userWrongAnsweRecords",query,PageData.class);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。