1.select * from user where name = "tom"
DBCollection dbCollection = getDb().getCollection(“user”);//数据表
DBObject first = new BasicDBObject();
first.put("name", "tom");
// 确定查询对象唯一,查询结果为object
DBObject findOne = dbCollection.findOne(first);
// 结果集搜索,查询结果为集合
DBCursor dbCollection1 = dbCollection.find(first);
2. select * from user where name = "tom" and age !=28
DBCollection dbCollection = getDb().getCollection(“user”);//数据表
DBObject basicDBObject = new BasicDBObject();
basicDBObject.put("name", "tom");
basicDBObject.put("age",new BasicDBObject("$ne", 28));
DBCursor cursor = dbCollection.find(basicDBObject);
3.select count(distinct name ) from user where age =23
DBCollection dbCollection = getDb().getCollection(“user”);//数据表
int count = dbCollection.distinct("name", new BasicDBObject("age", 23)).size();
4.select * from user where name = "tom" order by createTime
DBCollection dbCollection = getDb().getCollection(collectionName);
DBObject queryObj= new BasicDBObject();
queryObj.put("name","tom");
DBObject sortQuery = new BasicDBObject();//根据创建时间段倒序排列
sortQuery.put("createTime", -1);
cursor = dbCollection.find(queryObj).sort(sortQuery);
5.select * from user where ((name ="tom" and age =23) or (name="tom" and age =28) )and createTime > "2019-09-01" order by createTime dese
DBCollection dbCollection = getDb().getCollection("user");
DBObject queryObj = new BasicDBObject();
queryObj.put("name", "tom");
queryObj.put("age", 23);
DBObject basicDBObject = new BasicDBObject();
basicDBObject.put("name", "tom");
basicDBObject.put("age", 28);
BasicDBList basicDBList = new BasicDBList();
basicDBList.add(queryObj);
basicDBList.add(basicDBObject);
DBObject basicDBObject2 = new BasicDBObject();
basicDBObject2.put("$or", basicDBList);
basicDBObject2.put("createTime", new BasicDBObject("$gte","2019-09-01"));
cursor = dbCollection.find(basicDBObject2).sort(new BasicDBObject("createTime",-1));
6. select * from user where age not in (23,24) and name = "tom"
DBCollection dbCollection = getDb().getCollection(dbMap.get("user"));
DBObject queryObj = new BasicDBObject();
queryObj.put("name", "tom");
BasicDBList values = new BasicDBList();
values.add(23);
values.add(24);
queryObj.put("age", new BasicDBObject("$nin",values));
cursor = dbCollection.find(queryObj);
7. sum count 聚合
DBCollection dbCollection = getDb().getCollection(collectionName);//消息表
BasicDBObject where = new BasicDBObject();
where.put("时间范围", "三个月");
where.put("from", wxh);
where.put("to", hyzh);
DBObject match = new BasicDBObject("$match", where);//match该微信号的所有好友微信号
DBObject regroupFields = new BasicDBObject("_id", "$wxh");// 根据哪个字段
if( 1 == type){
regroupFields.put("num" ,new BasicDBObject("$sum", "$ltju"));//聊天次数sum
}else if(2==type){
regroupFields.put("num" , new BasicDBObject("$count", "$ltts"));//聊天天数count
}
DBObject regroup = new BasicDBObject("$group", regroupFields);//$group
List<DBObject> ops = new ArrayList<>();
ops.add(match);
ops.add(regroup);
this.outputOne = dbCollection.aggregate(ops);
AggregationOutput output = dbCollection.aggregate(ops);
Iterator<DBObject> iterator = output.results().iterator();