//新建Query对象
Query query = new Query();
//创建Criteria对象,匹配查询条件
Criteria criteria = new Criteria() {
@Override
public document getCriteriaObject() {
document doc = new document();
doc.put("$where", "this.lastUpdateTime > this.lastReadTime");
return doc;
}
}
query.addCriteria(criteria);
//后面可以加上query.addCriteria(...),实现多条件操作。
//查询用户信息
List userList = mongoTemplate.find(query, User.class)
//查询总数
query.fields().include("_id");
long totalCount = mongoTemplate.find(query, User.class).size();
需要注意的是$where本身不能使用count,会报错 $whereis not allowed in this context,此处上下文不允许使用 $where,如果要达到统计多少条的目的,查询出来之后,判断长度,可以只返回的_id以减少传输的数据量,提高查询效率。
Command failed with error 2 (BadValue): '$where is not allowed in this context' on server 127.0.0.1:27017. The full response is {"ok": 0.0, "errmsg": "$where is not allowed in this context



