Main Content

count

Count total number of documents in MongoDB collection

Since R2021b

Description

n = count(conn,collection) returns the total number of documents in a collection by using the MongoDB® C++ interface connection.

example

n = count(conn,collection,Query=mongoquery) returns the total number of documents in an executed MongoDB query on a collection.

example

Examples

collapse all

Connect to MongoDB® using the MongoDB C++ interface and count the total number of documents in a collection.

Create a MongoDB connection to the database mongotest using the MongoDB C++ interface. Here, the database server dbtb01 hosts this database using port number 27017.

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongoc(server,port,dbname)
conn = connection with properties:
           Database: "mongotest"
           UserName: ""
             Server: "dbtb01"
               Port: 27017
    CollectionNames: [13×1 string]

conn is the connection object that contains the MongoDB connection. The object properties contain information about the connection and the database.

  • The database name is mongotest.

  • The user name is blank.

  • The database server is dbtb01.

  • The port number is 27017.

  • This database contains 13 document collections.

Verify the MongoDB connection.

isopen(conn)
ans = logical
   1

The database connection is successful because the isopen function returns 1. Otherwise, the database connection is closed.

Determine the number of documents in the employees collection. The collection contains seven documents.

collection = "employees";
n = count(conn,collection)
n = int64
    7

Close the MongoDB connection.

close(conn)

Connect to MongoDB® using the MongoDB C++ interface and count the total number of documents in a MongoDB query on a collection in the database. Here, each document in the collection represents an employee.

Create a MongoDB connection to the database mongotest using the MongoDB C++ interface. Here, the database server dbtb01 hosts this database using port number 27017.

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongoc(server,port,dbname)
conn = connection with properties:
           Database: "mongotest"
           UserName: ""
             Server: "dbtb01"
               Port: 27017
    CollectionNames: [13×1 string]

conn is the connection object that contains the MongoDB connection. The object properties contain information about the connection and the database.

  • The database name is mongotest.

  • The user name is blank.

  • The database server is dbtb01.

  • The port number is 27017.

  • This database contains 13 document collections.

Verify the MongoDB connection.

isopen(conn)
ans = logical
   1

The database connection is successful because the isopen function returns 1. Otherwise, the database connection is closed.

Create a JSON-style query as a string scalar that contains a JSON-style string. This query sets the department identifier field equal to 80.

mongoquery = "{""department_id"":80}";

Use the MongoDB query on the employees collection to count the total number of employees who work in the specified department. A total of four employees work in the department.

collection = "employees";
n = count(conn,collection,Query=mongoquery)
n = int64
    4

Close the MongoDB connection.

close(conn)

Input Arguments

collapse all

MongoDB C++ interface connection, specified as a connection object.

Collection name, specified as a string scalar.

Example: "taxidata"

Data Types: string

MongoDB query, specified as a string scalar or character vector. Specify a JSON-style string to query the database.

Example: "{""department"":""Sales""}" queries the database for documents where the department field is equal to Sales.

Example: "{""salary"": {""$gt"": 90000}}" queries the database for documents where the value of the salary field is greater than 90000.

Data Types: string | char

Output Arguments

collapse all

Total number of documents in a MongoDB collection or query, returned as an int64 scalar.

Version History

Introduced in R2021b