Main Content

mongoc

Create MongoDB C++ interface connection

Description

example

conn = mongoc(server,port,dbname) creates a MongoDB® connection using the MongoDB C++ interface with the database server, port number, and database name.

example

conn = mongoc(server,port,dbname,Name=Value) specifies additional options using one or more name-value arguments. For example, UserName="adminuser" specifies the user name for the connection.

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 collection. Specify a user name and password to connect to the database.

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. Specify the user name adminuser and password matlab by setting the UserName and Password name-value arguments, respectively.

conn = mongoc("dbtb01",27017,"mongotest",UserName="adminuser",Password="matlab")
conn = 

  connection with properties:

               Database: "mongotest"
               UserName: "adminuser"
                 Server: "dbtb01"
                   Port: 27017
        CollectionNames: [13×1 string]

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

  • The database name is mongotest.

  • The user name is adminuser.

  • The database server is dbtb01.

  • The port number is 27017.

  • This database contains 13 document collections.

Check the MongoDB C++ interface 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 =

    7

Close the MongoDB C++ interface connection.

close(conn)

Input Arguments

collapse all

Server name, specified as a string scalar for one database server name or a string array for multiple database server names.

Example: "localhost"

Data Types: string

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

Example: 27017

Data Types: double

Database name, specified as a string scalar.

Example: "employeesdb"

Data Types: string

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: conn = mongoc(server,port,dbname,UserName="username",Password="pwd") creates a MongoDB C++ interface connection using the specified user name and password.

User name, specified as a string scalar. Contact your MongoDB administrator for access credentials.

If you specify the UserName name-value argument, then you must also specify the Password name-value argument.

Example: "username"

Data Types: string

Password, specified as a string scalar. Contact your MongoDB administrator for access credentials.

If you specify the Password name-value argument, then you must also specify the UserName name-value argument.

Example: "pwd"

Data Types: string

SSL-enabled connection, specified as the value false or true. Setting this argument to true creates an SSL-enabled connection to MongoDB.

Data Types: logical

Output Arguments

collapse all

MongoDB C++ connection, returned as a connection object.

Introduced in R2021b