Tacocat
Tacocat

Reputation: 1134

MongoDB Stats Count Different from Number of Documents

Robomongo shows that I have 50 documents in my database's collection (which is true), and yet, the console output from the following says otherwise; count() returns 4494 and so does stats(). This is causing my cursor to erroneously iterate through the collection repeatedly instead of just once.

MongoClient mongoClient = new MongoClient(DATABASE_HOST, DATABASE_PORT);

DB db = mongoClient.getDB(DATABASE_NAME);

DBCollection documentCollection = db.getCollection(COLLECTION_NAME);

DBCursor cursor = documentCollection.find();

System.out.println("Number of documents in collection: " + cursor.count());

Please help! Sincerely confused.

P.S. I have used validate() to confirm that all documents in the collection are valid. Also, documentCollection.getCount() returns the same value of 4494.

Upvotes: 1

Views: 262

Answers (2)

Alex Blex
Alex Blex

Reputation: 37128

Robomongo paginates results, 50 documents per page by default.
db.COLLECTION_NAME.count() returns total number of documents in the collection.

Upvotes: 1

Try with:

MongoCollection<Document> collection;
collection.count();

Upvotes: 0

Related Questions