Navigation

collection.count()

Definition

collection.count()

Return the number of documents in a collection or view that match the specified query filter.

Usage

Example

To call the collection.count() action from a Function, get a collection handle with database.collection() then call the handle’s count() method.

collection.count({ status: "Complete" })
  .then(numDocs => console.log(`${numDocs} documents match the specified query.`))
  .catch(err => console.error("Failed to count documents: ", err))

To call the collection.count() action from a JavaScript SDK, use the RemoteMongoCollection.count() method.

collection.count({ status: "Complete" })
  .then(numDocs => console.log(`${numDocs} documents match the specified query.`))
  .catch(err => console.error("Failed to count documents: ", err))

To call the collection.count() action from the Java/Android SDK, use the RemoteMongoCollection.count() method.

collection.count(new Document("status", "Complete"))
  .addOnCompleteListener(new OnCompleteListener <Long> () {
    @Override
    public void onComplete(@NonNull Task <Long> task) {
      if (task.isSuccessful()) {
        Long numDocs = task.getResult();
        Log.d("app", String.format(
          "%s documents match the specified query.",
          numDocs.toString()
        ));
      } else {
        Log.e("app", "Failed to count documents with exception: ", task.getException());
      }
    }
  });

To call the collection.count() action from the Swift/iOS SDK, use the RemoteMongoCollection.count() method.

collection.count(["status", "Complete"]) { result in
  switch result {
  case .success(let numDocs):
    print("\(numDocs) documents match the specified query.")
  case .failure(let error):
    print("Failed to count documents: ", error)
  }
}

Parameters

The collection.count() action has the following form:

count(query)
Parameter Description

Query Filter

query: <document>

Optional. A standard MongoDB query document that specifies which documents to count. You can use most query selectors except for evaluation, geospatial, or bitwise selectors.

Specify an empty query filter ({}) or omit this parameter to count all documents in the collection.

The count() method has the following form:

count(query)
Parameter Description

Query Filter

query: <document>

Optional. A standard MongoDB query document that specifies which documents to count. You can use most query selectors except for evaluation, geospatial, or bitwise selectors.

Specify an empty query filter ({}) or omit this parameter to count all documents in the collection.

The count() method has the following form:

count(query)
Parameter Description

Query Filter

query: <document>

Optional. A standard MongoDB query document that specifies which documents to count. You can use most query selectors except for evaluation, geospatial, or bitwise selectors.

Specify an empty query filter ({}) or omit this parameter to count all documents in the collection.

The count() method has the following form:

count(query)
Parameter Description

Query Filter

query: <document>

Optional. A standard MongoDB query document that specifies which documents to count. You can use most query selectors except for evaluation, geospatial, or bitwise selectors.

Specify an empty query filter ({}) or omit this parameter to count all documents in the collection.

Return Value

The collection.count() action returns a Promise that resolves to the integer number of documents in the collection that match the query filter.

Promise<numDocs: Number>

The RemoteMongoCollection.count() method returns a Promise that resolves to the integer number of documents in the collection that match the query filter.

Promise<numDocs: Number>

The RemoteMongoCollection.count() method returns a Task that resolves to the integer number of documents in the collection that match the query filter.

Task<Long numDocs>

The RemoteMongoCollection.count() method returns a StitchResult enum that resolves to the integer number of documents in the collection that match the query filter.

enum StitchResult {
  case success(numDocs: Int)
  case failure(error: StitchError)
}
Value Description

Count Result

numDocs: <integer>
numDocs: <integer>
numDocs: <Long>
numDocs: <Int>
The number of documents in the collection that match the provided query filter.