BasicDBObject查询MongoDB
作者:互联网
1.查询举例
//查询条件设置 BasicDBObject queryCond = new BasicDBObject(); //单个字段 queryCond.put("data_source", "诊断记录"); //in 的用法 queryCond.put("patient_id", new BasicDBObject("$in", patientIdList)); //数组字段中匹配多个符合条件的值 queryCond.put("data", new BasicDBObject("$elemMatch", new BasicDBObject("key", "EGFR").append("value", "+"))); //exists 用法 1 代表存在该字段,字段值为 null的也算 queryCond.put("time_point_id", new BasicDBObject("$exists", 1)); //指定返回字段 0代表不返回,1代表返回,_id会默认返回(不返回需手动设置为0) BasicDBObject filterCond = new BasicDBObject(); filterCond.put("_id", 0); filterCond.put("patient_id", 1); filterCond.put("data", 1); //设置字段排序 1表示升序,-1表示倒序 BasicDBObject sortCond = new BasicDBObject(); sortCond.put("survey_time", 1); //查询结果 MongoCursor<Document> cursor = mongoTemplate.getCollection("collection_name").find(queryCond).projection(filterCond).sort(sortCond).iterator(); //遍历结果 while (cursor.hasNext()) { Document doc = cursor.next(); Object pidObj = doc.get("patient_id"); }
2. Mongo连表查询
(17条消息) mongodb联表查询_浅夏晴空的博客-CSDN博客_mongodb联表查询
标签:BasicDBObject,MongoDB,id,查询,filterCond,put,new,queryCond 来源: https://www.cnblogs.com/JNU-Iot-Longxin/p/16591713.html