Main Content


Determine if MongoDB connection is open

The isopen function will be removed in a future release. Use the isopen function of the MongoDB® C++ interface instead.



i = isopen(conn) returns 1 if the MongoDB connection is open and 0 if it is closed.


collapse all

Connect to MongoDB and count the total number of documents in a collection.

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

server = "dbtb01";
port = 27017;
dbname = "mongotest";
conn = mongo(server,port,dbname)
conn = 

  mongo with properties:

               Database: 'mongotest'
               UserName: ''
                 Server: {'dbtb01'}
                   Port: 27017
        CollectionNames: {'airlinesmall', 'employee', 'largedata' ... and 3 more}
         TotalDocuments: 23485919

conn is the mongo 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 six document collections. The first three collection names are airlinesmall, employee, and largedata.

  • This database contains 23,485,919 documents.

Verify the MongoDB connection.

ans =



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

Determine the number of documents in the employee collection. There are 25 documents in the collection.

collection = "employee";
n = count(conn,collection)
n =


Close the MongoDB connection.


Verify the MongoDB connection is closed.

ans =



Input Arguments

collapse all

MongoDB connection, specified as a mongo object.

Version History

Introduced in R2017b