MongoDB笔记1

MongoDB使用java-driver-3.0.0时控制输出的field可以使用projection()方法,这个与之前的版本不同。


BasicDBObject keys1 = new BasicDBObject();

//读取数据库的数据,直接输出JSON格式数据。。。
//or 查询
// keys1.put("$or", asList(new Document("date","2015010120"),new Document("temp"," 8")));
// keys1.put("date",new BasicDBObject("$gte","2015010100").append("$lte","2015010300"));
// keys1.put("temp",new BasicDBObject("$gte","2015010200"));
keys1.put("$or", asList(new Document("Control","废气国控"),new Document("Control","废气")));



//控制显示的fields
BasicDBObject keys2 = new BasicDBObject();
keys2.put("_id", false);
// keys2.put("date", true);
// keys2.put("temp", true);
keys2.put("lng", true);
keys2.put("lat", true);
keys2.put("CompanyName",true);
//控制数据排序,-1是大到小,1是小到大
BasicDBObject keys3 = new BasicDBObject();
keys3.put("date", 1);

FindIterable findIterable = collection
 .find(keys1)
 .projection(keys2)
 .sort(keys3);

你可能感兴趣的:(数据库,mongodb)