1. query.fields().include("idfa").include("imei").include("id").include("os"); 返回数据中包含这几个字段
2.
①List<Criteria> criterias = new ArrayList<>();
criterias.add(Criteria.where("time").gt("2017-09-20 10:02:26"));
criterias.add(Criteria.where("time").lt("2017-09-27 10:02:26"));
criterias.add(Criteria.where("imei").is("9686854123"));
query.addCriteria(new Criteria().andOperator(criterias.toArray(new Criteria[0]))); ②toArray解释 public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.
newInstance(a.getClass().getComponentType(), size);
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
由①得出如下查询语句
{ "channel" : "jinritoutiao" , "$and" : [ { "imei" : "96868541236"} , { "time" : { "$lt" : "2017-09-27 10:02:26"}} , { "time" : { "$gt" : "2017-09-20 10:02:26"}} ]}
3.
query.addCriteria(Criteria.where(“Key”).is(”Value“));
从下面源码可以看出上面中的key不能是重复的值 public Query addCriteria(CriteriaDefinition criteriaDefinition) { CriteriaDefinition existing = (CriteriaDefinition)this.criteria.get(criteriaDefinition.getKey()); String key = criteriaDefinition.getKey(); if (existing == null) { this.criteria.put(key, criteriaDefinition); return this; } else { throw new InvalidMongoDbApiUsageException("Due to limitations of the com.mongodb.BasicDBObject, you can‘t add a second ‘" + key + "‘ criteria. " + "Query already contains ‘" + existing.getCriteriaObject() + "‘."); } }
时间: 2024-10-13 19:42:21