mongo

MongoDB connection

Description

The mongo function creates a mongo object using the Database Toolbox™ interface for MongoDB®. With the object, you can connect to MongoDB stored on one or more database servers.

First, you must install the Database Toolbox interface for MongoDB. For details, see Database Toolbox Interface for MongoDB Installation.

Using the mongo object, you can manage 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

Description

example

conn = mongo(server,port,dbname) creates a MongoDB connection to the database server using a database name and sets the Port property.

example

conn = mongo(server,port,dbname,Name,Value) specifies additional options using one or more name-value pair arguments. For example, 'SSLEnabled',true creates an SSL-enabled connection to MongoDB.

Input Arguments

expand 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

Database name, specified as a string scalar.

Example: "employeesdb"

Data Types: string

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

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

User name, specified as the comma-separated pair consisting of 'UserName' and a string scalar. Contact your MongoDB administrator for access credentials.

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

Example: "username"

Data Types: string

Password, specified as the comma-separated pair consisting of 'Password' and a string scalar. Contact your MongoDB administrator for access credentials.

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

Example: "pwd"

Data Types: string

SSL-enabled connection, specified as the comma-separated pair consisting of 'SSLEnabled' and the value false or true. Setting this argument to true creates an SSL-enabled connection to MongoDB.

Data Types: logical

Properties

expand all

Database name, specified as a character vector.

The dbname input argument 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 pair argument sets this property.

Example: 'username'

Data Types: char

This property is read-only.

Server name, specified as a cell array of character vectors. Each character vector in the cell array specifies one database server name.

The server input argument sets this property.

Example: {'server1'}

Data Types: cell

This property is read-only.

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

Example: 27017

Data Types: double

This property is read-only.

Collection names of all collections defined in MongoDB, specified as a cell array of character vectors.

Example: {'airlinesmall', 'employee', 'largedata' ... and 3 more}

Data Types: cell

This property is read-only.

Count of the documents in all collections defined in MongoDB, specified as a numeric scalar.

Data Types: double

Object Functions

expand all

isopenDetermine if MongoDB connection is open
closeClose MongoDB connection
countCount total number of documents in MongoDB collection
distinctRetrieve distinct values for field 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 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.

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 employee collection. The collection contains 25 documents.

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

    25

Close the MongoDB connection.

close(conn)

Connect to MongoDB 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. 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 pair arguments, respectively.

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

  mongo with properties:

               Database: 'mongotest'
               UserName: 'adminuser'
                 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 adminuser.

  • 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.

Check 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 employee collection. The collection contains 25 documents.

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

    25

Close the MongoDB connection.

close(conn)

Connect to MongoDB and count the total number of documents in a collection. Specify a user name and password to connect to the database. Create an SSL-enabled connection.

Create a MongoDB connection to the database mongotest. 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 pair arguments, respectively. Create an SSL-enabled connection by setting the 'SSLEnabled' name-value pair argument to true.

conn = mongo("dbtb01",27017,"mongotest",'UserName',"adminuser",'Password',"matlab", ...
    'SSLEnabled',true)
conn = 

  mongo with properties:

               Database: 'mongotest'
               UserName: 'adminuser'
                 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 adminuser.

  • 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.

Check 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 employee collection. The collection contains 25 documents.

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

    25

Close the MongoDB connection.

close(conn)

Connect to MongoDB and count the total number of documents in a collection. Specify a user name and password to connect to the database. Then, specify another database and perform another count of a collection.

Create a MongoDB connection to the database mongotest. 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 pair arguments, respectively.

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

  mongo with properties:

               Database: 'mongotest'
               UserName: 'adminuser'
                 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 adminuser.

  • 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.

Check 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 employee collection. There are 25 documents in the collection.

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

    25

Specify another database named otherdb using dot notation.

conn.Database = "otherdb";

Determine the number of documents in the company collection. The collection contains five documents.

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

    5

Close the MongoDB connection.

close(conn)
Introduced in R2017b