@Test
public void query() {
//查询所有
//queryAll();
//查询id = 4de73f7acd812d61b4626a77
print("find id = 4de73f7acd812d61b4626a77: " + users.find(new BasicDBObject("_id", new ObjectId("4de73f7acd812d61b4626a77"))).toArray());
//查询age = 24
print("find age = 24: " + users.find(new BasicDBObject("age", 24)).toArray());
//查询age >= 24
print("find age >= 24: " + users.find(new BasicDBObject("age", new BasicDBObject("$gte", 24))).toArray());
print("find age <= 24: " + users.find(new BasicDBObject("age", new BasicDBObject("$lte", 24))).toArray());
print("查询age!=25:" + users.find(new BasicDBObject("age", new BasicDBObject("$ne", 25))).toArray());
print("查询age in 25/26/27:" + users.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.IN, new int[] { 25, 26, 27 }))).toArray());
print("查询age not in 25/26/27:" + users.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.NIN, new int[] { 25, 26, 27 }))).toArray());
print("查询age exists 排序:" + users.find(new BasicDBObject("age", new BasicDBObject(QueryOperators.EXISTS, true))).toArray());
print("只查询age属性:" + users.find(null, new BasicDBObject("age", true)).toArray());
print("只查属性:" + users.find(null, new BasicDBObject("age", true), 0, 2).toArray());
print("只查属性:" + users.find(null, new BasicDBObject("age", true), 0, 2, Bytes.QUERYOPTION_NOTIMEOUT).toArray());
//只查询一条数据,多条去第一条
print("findOne: " + users.findOne());
print("findOne: " + users.findOne(new BasicDBObject("age", 26)));
print("findOne: " + users.findOne(new BasicDBObject("age", 26), new BasicDBObject("name", true)));
//查询修改、删除
print("findAndRemove 查询age=25的数据,并且删除: " + users.findAndRemove(new BasicDBObject("age", 25)));
//查询age=26的数据,并且修改name的值为Abc
print("findAndModify: " + users.findAndModify(new BasicDBObject("age", 26), new BasicDBObject("name", "Abc")));
print("findAndModify: " + users.findAndModify(
new BasicDBObject("age", 28), //查询age=28的数据
new BasicDBObject("name", true), //查询name属性
new BasicDBObject("age", true), //按照age排序
false, //是否删除,true表示删除
new BasicDBObject("name", "Abc"), //修改的值,将name修改成Abc
true,
true));
queryAll();
}