mongo
MongoDB connection
The mongo
function will be removed in a future release. Use the
mongoc
function of the MongoDB® C++ interface instead.
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
specifies additional options using one or more name-value pair arguments. For
example, conn
= mongo(server
,port
,dbname
,Name,Value
)'SSLEnabled',true
creates an SSL-enabled connection
to MongoDB.
Input Arguments
server
— Server name
string scalar | string array
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
dbname
— Database name
string scalar
Database name, specified as a string scalar.
Example: "employeesdb"
Data Types: string
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.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: conn =
mongo(server,port,dbname,'UserName',"username",'Password',"pwd")
creates a MongoDB connection using the specified user name and
password.
UserName
— User name
string scalar
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
— Password
string scalar
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
SSLEnabled
— SSL-enabled connection
false
(default) | true
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
Database
— Database name
character vector
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
UserName
— User name
character vector
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
Server
— Server name
cell array of character vectors
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
Port
— Port number
numeric scalar | numeric vector
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
CollectionNames
— Collection names
cell array of character vectors
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
TotalDocuments
— Count of documents in all collections
numeric scalar
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
Import Document Collections into MATLAB
Export and Manage Document Collections in MongoDB
createCollection | Create MongoDB collection |
dropCollection | Drop MongoDB collection |
insert | Insert one or multiple documents into MongoDB collection |
remove | Remove one or multiple documents from MongoDB collection |
update | Update one or multiple documents in MongoDB collection |
Examples
Create MongoDB Connection
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
, andlargedata
.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)
Create MongoDB Connection Using User Name and Password
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
, andlargedata
.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)
Create MongoDB SSL-Enabled Connection
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
, andlargedata
.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)
Switch to Another Database
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
, andlargedata
.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)
Version History
See Also
Topics
External Websites
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)