我不能爲我的生活找出如何獲得在mongo數據庫中使用java驅動程序查找查詢的計數。有人能讓我擺脫我的痛苦嗎?MongoDB - 如何獲得查詢查詢的計數
我有以下幾點:
MongoCursor<Document> findRes = collection.find().iterator();
但是沒有,我可以找到任何地方的計數方法。
我不能爲我的生活找出如何獲得在mongo數據庫中使用java驅動程序查找查詢的計數。有人能讓我擺脫我的痛苦嗎?MongoDB - 如何獲得查詢查詢的計數
我有以下幾點:
MongoCursor<Document> findRes = collection.find().iterator();
但是沒有,我可以找到任何地方的計數方法。
public Long getTotalCount(String collectionName, Document filterDocument) {
MongoCollection collection = database.getCollection(collectionName);
return filterDocument != null ? collection.count(filterDocument) : collection.count();
}
哪裏filterDocument是org.bson.Document與過濾審覈規定或無效,如果你想獲得總計數
您也可以使用更強大的Filters
類。例如:collection.count(Filters.and(Filters.eq("field","value"),second condition and so on));
因此,爲了能夠利用這兩個文件和過濾器作爲PARAM您可以更改簽名public Long getTotalCount(String collectionName, Bson filterDocument) {
cursor.count()
是你在找我相信。您的查詢查詢返回Cursor
,因此您可以在此處致電count()
。
它不存在。我沒有看到遊標的計數方法。我使用MongoCursor
構建您的查詢並將其傳遞到您的集合上的find()。它返回一個'DBCursor',它應該有一個'count()'(至少它上次是我檢查過的)。 – Oli
它似乎已在最新版本中發生變化。現在你創建你的查詢並將其傳遞給一個計數。因此,而不是collection.find(
MongoDB中已經內置的方法count()
,可以調用上的光標找到返回的文檔數量。 我嘗試以下MongoDB中的一段代碼,運行良好,可以很容易地在Java或任何其他語言也適用於:
var findres = db.c.find() findres.count()
給輸出29353
long rows = db.getCollection(myCollection).count(new Document("_id", 10)) ;
這是在Java中,myCollection
是集合名稱。
乾杯的人幫了很多。謝謝 – discodowney