lukisp
lukisp

Reputation: 1081

Mongo count on collection or count on cursor - which faster

I have question which counting method is faster: I have some filter parameters called filterParameters. Now I doing:

1. First option:
  int count = count with db.dbCollection.count(filterParameters) 
  and then 
  dbCursor = db.dbCollection.find(filterParameters).skip(..).limit(..)

2. Second way:
  dbCursor = db.dbCollection.find(filterParameters).skip(..).limit(..)
  int count = dbCursor.count()

And in Java, first way:

Integer countAllItems = documentService.count(documentType,filterQuery)
DBCursor documents = documentService.list(documentType, [:], filterQuery, sortQuery, command.start, command.count)

where: documentService.count(documentType,filterQuery) doing:

def collection = db.getCollection(documentType.collectionName)
collection.count(filterQuery)

and documentService.list is only find on collection.

And then second way:

DBCursor documents = documentService.list(documentType, [:], filterQuery, sortQuery, command.start, command.count)
Integer countAllItems = documents.count()

Which one is faster method ?

Upvotes: 0

Views: 293

Answers (1)

Sammaye
Sammaye

Reputation: 43884

Neither, the particular count you are using is a symlink for the cursor count, they are the same function.

Upvotes: 2

Related Questions