Main Content

connection

MongoDB C++ interface connection

Description

The connection object enables you to connect to MongoDB® stored on one or more database servers. Using the connection object, you can manage document collections in the database. You can also query documents stored in a collection and import them into the MATLAB® workspace. From MATLAB, you can export MATLAB tables, structures, and objects into MongoDB. For details about MongoDB, see the MongoDB Manual.

Creation

Create the connection object using the mongoc function.

Properties

expand all

Database name, specified as a character vector.

The dbname input argument of the mongoc function sets this property.

To change the name of the database, use dot notation to set this property; for example:

conn.Database = "otherDatabase";

Example: "databasename"

Data Types: char

This property is read-only.

User name, specified as a character vector.

The UserName name-value argument of the mongoc function sets this property.

Example: "username"

Data Types: char

This property is read-only.

Server name, specified as a string scalar.

The server input argument of the mongoc function sets this property.

Example: "server1"

Data Types: string

This property is read-only.

Port number, specified as a numeric scalar for one port or a numeric vector for multiple ports.

The port input argument of the mongoc function sets this property.

Example: 27017

Data Types: double

This property is read-only.

Collection names of all collections defined in MongoDB, specified as a string scalar for one collection or string array for multiple collections.

Example: [13×1 string]

Data Types: string

Object Functions

expand all

isopenDetermine if MongoDB C++ interface connection is open
closeClose MongoDB C++ interface connection
countCount total number of documents in MongoDB collection
findRetrieve documents in MongoDB collection
createCollectionCreate MongoDB collection
dropCollectionDrop MongoDB collection
insertInsert one or multiple documents into MongoDB collection
removeRemove one or multiple documents from MongoDB collection
updateUpdate one or multiple documents in MongoDB collection

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)
Introduced in R2021b