Determine if MongoDB C++ interface connection is open
Verify MongoDB C++ Interface Connection
Connect to MongoDB® using the MongoDB C++ interface and count the total number of documents in a collection. Then, verify the connection is closed.
Create a MongoDB connection to the database
mongotest using the MongoDB C++ interface. Here, the database server
dbtb01 hosts this database using port number
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
The user name is blank.
The database server is
The port number is
This database contains 13 document collections.
Verify the MongoDB connection.
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.
Verify the MongoDB connection is closed.
ans = logical 0
Introduced in R2021b