參考:www.cnblogs.com/bookc/p/9887922.ht...
MongoDB 查詢有四種方式:Query,TextQuery,BasicQuery 和 Bson ,網上太多關於 Query 的查詢方式,本文只記錄 BasicQuery和Bson 的方式,BasicQuery 相對於 Query 更加的靈活,BasicQuery 就是 Query 的擴充套件,BasicQuery 可以返回指定列資料。最靈活的是Bson方式。
大寫的採坑經驗:
1.MongoDB 雖然儲存很靈活,但是,不要儲存Map型別的,不要儲存Map型別的,不要儲存Map型別的。儘量儲存強型別的,儘量儲存強型別的,儘量儲存強型別的。如果有Map型別的,對於查詢指定列的,只能用Bson ,如果是強型別的,就可以直接用Query,TextQuery。
2.複雜型別查詢 可以考慮 Bson 方式,Bson 請參考文章最後。
安裝Maven包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
測試資料程式碼
for (Long i = 0L; i < 10L; i++) {
MongoDBTestVo vo=new MongoDBTestVo();
vo.setUserId(i);
vo.setEmail("1@1.com");
vo.setName("test"+i.toString());
vo.setPhone(i.toString());
vo.setCreateDate(new Date());
mongoTemplate.insert(vo,"mongodbtest");
}
MongoDBTestVo Code
單條件查詢
DBObject obj = new BasicDBObject();
obj.put("userId", new BasicDBObject("$gte", 2)); // userId>=2的條件
//obj.put("userId", 2); userId=2 的條件
Query query = new BasicQuery(obj.toString());
List<MongoDBTestVo> result = mongoTemplate.find(query, MongoDBTestVo.class, "mongodbtest");
多條件查詢
BasicDBList basicDBList = new BasicDBList();
basicDBList.add(new BasicDBObject("userId", 2L));
basicDBList.add(new BasicDBObject("name","test2"));
DBObject obj = new BasicDBObject();
obj.put("$and", basicDBList);
Query query = new BasicQuery(obj.toString());
List<MongoDBTestVo> result = mongoTemplate.find(query, MongoDBTestVo.class, "mongodbtest");
檢視指定列的資料
DBObject obj = new BasicDBObject();
obj.put("userId", new BasicDBObject("$gte", 2));
BasicDBObject fieldsObject = new BasicDBObject();
fieldsObject.put("userId", 1);
fieldsObject.put("name", 1);
Query query = new BasicQuery(obj.toString(), fieldsObject.toString());
List<Map> result = mongoTemplate.find(query, Map.class, "mongodbtest");
BasicQuery查詢語句可以指定返回欄位,建構函式BasicQuery(DBObject queryObject, DBObject fieldsObject),fieldsObject 這個欄位可以指定返回欄位
fieldsObject.put(key,value)
key:欄位
value:
說明:
1或者true表示返回欄位
0或者false表示不返回該欄位
_id:預設就是1,沒指定返回該欄位時,預設會返回,除非設定為0是,就不會返回該欄位。
指定返回欄位,有時文件欄位多並資料大時,我們指定返回我們需要的欄位,這樣既節省傳輸資料量,減少了記憶體消耗,提高了效能,在資料大時,效能很明顯的。
Bson查詢方式
Bson bson = Filters.and(Arrays.asList(
Filters.gte("createdTime", Calendar.getInstance().getTime()),
Filters.gte("apis.count", 1)));
MongoCursor<Document> cursor = null; try {
FindIterable<Document> findIterable = mongoTemplate.getCollection("mongodbtest").find(bson);
cursor = findIterable.iterator();
List<MongoDBTestVo> list = new ArrayList<>(); while (cursor.hasNext()) {
Document object = cursor.next();
MongoDBTestVo entity = JSON.parseObject(object.toJson(), MongoDBTestVo.class);
list.add(entity);
} return list;
} finally { if (cursor != null) {
cursor.close();
}
}
Bson返回指定列
Bson bson = Filters.and(Arrays.asList(
Filters.gte("createdTime", Calendar.getInstance().getTime()),
Filters.gte("apis.count", 1)));
BasicDBObject fieldsObject = new BasicDBObject();
fieldsObject.put("apis.count", 1);
fieldsObject.put("_id", 0);
MongoCursor<Document> cursor = null; try {
FindIterable<Document> findIterable = mongoTemplate.getCollection("mongodbtest").find(bson).projection(Document.parse(fieldsObject.toString()));
cursor = findIterable.iterator();
List<MongoDBTestVo> list = new ArrayList<>(); while (cursor.hasNext()) {
Document object = cursor.next();
MongoDBTestVo entity = JSON.parseObject(object.toJson(), MongoDBTestVo.class);
list.add(entity);
} return list;
} finally { if (cursor != null) {
cursor.close();
}
}
Bson 排序
Bson bson = Filters.and(Arrays.asList(
Filters.gte("createdTime", Calendar.getInstance().getTime()),
Filters.gte("apis.count", 1)));
BasicDBObject fieldsObject = new BasicDBObject();
fieldsObject.put("apis.count", 1);
fieldsObject.put("_id", 0);
BasicDBObject sort = new BasicDBObject();
sort.put("createdTime", 1);
MongoCursor<Document> cursor = null;
try {
FindIterable<Document> findIterable = mongoTemplate.getCollection("mongodbtest").find(bson).projection(Document.parse(fieldsObject.toString())).sort(sort);
cursor = findIterable.iterator();
List<MongoDBTestVo> list = new ArrayList<>();
while (cursor.hasNext()) {
Document object = cursor.next();
MongoDBTestVo entity = JSON.parseObject(object.toJson(), MongoDBTestVo.class);
list.add(entity);
}
return list;
} finally {
if (cursor != null) {
cursor.close();
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結