FindIterable <Document> how to get summary records as a result of a query

I used the mongo driver for java 2.x and passed 3.1. DBCursordeprecated and I had to use a structure FindIterable<Document>.

When I execute the request

FindIterable<Document> cursor=(mongo).getCollection().find(findArgs).limit(10).skip(0);

I would like to get a counter of the total number of records in the result set with query search in only one query.

How to get a counter without executing another request? I'm not talking about 10 entries in the limit, but about the total number of entries.

I want to fulfill one request for invoice and search. I do not want to search and count.

thanks

+6
source share
6 answers

. Bson. count. MongoCollection = db.getCollection(collectionName); long totalFilteredRecords = collection.count( "", "" );

+1

:

(mongo).getCollection().count(findArgs);
+1

, , , , , , - , . Querying "Count"

mongoClient = new MongoClient("localhost", 27017);
              DB db = mongoClient.getDB("DatabaseName");
              DBCollection coll = db.getCollection("CollectionName");
              long count = coll.count();

, !

0

MongoCollection.count() . :

long result = collection.countDocuments(findArgs);

long result = collection.countDocuments(eq("_id", idValue));
0

FindIterable - . . - " ", ( ).

():

Map query = [_id: [$in:idList]]

FindIterable userList = User.collection.find(query).skip(offset).limit(max)

Integer totalCount = User.collection.count(query)

, , , , , , .

- (mongodb version 3. 4+), , .

0

. - count(). , .

count(), MongoDB , . , , .

, MongoDB . , , . , count().

, Iterable size (, guava), :

Iterable<?> it = table.find(filterBson).projection(new Document("_id", 1));
int count = Iterables.size(it);
0

Source: https://habr.com/ru/post/1616237/


All Articles