Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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

Syntax

conn = mongo(server,port,dbname)
conn = mongo(server,port,dbname,'UserName',username,'Password',password)

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,'UserName',username,'Password',password) specifies a user name and password for connecting 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

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

Example: "username"

Data Types: string

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

Example: "pwd"

Data Types: string

Properties

expand all

This property is read-only.

Database name, specified as a character vector.

Example: 'databasename'

Data Types: char

This property is read-only.

User name, specified as a character vector.

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.

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

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

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

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.

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

Close the MongoDB connection.

close(conn)

Introduced in R2017b

Was this topic helpful?