mongoc
Description
Examples
Create MongoDB C++ Interface Connection
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)
Create MongoDB Connection Using User Name and Password
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
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
port
— Port number
numeric scalar | numeric vector
Port number, specified as a numeric scalar for one port or a numeric vector for multiple ports.
Example: 27017
Data Types: double
dbname
— Database name
string scalar
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.
UserName
— User name
string scalar
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
— Password
string scalar
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
Output Arguments
conn
— MongoDB C++ connection
connection
object
MongoDB C++ connection, returned as a connection
object.
Version History
Introduced in R2021b
See Also
Objects
Functions
Topics
- Import and Analyze Data from MongoDB Using MongoDB C++ Interface
- Import Filtered Data from MongoDB Using MongoDB C++ Interface
- Import Large Data from MongoDB Using MongoDB C++ Interface
- Export MATLAB Data into MongoDB Using MongoDB C++ Interface
- Import and Export MATLAB Objects Using MongoDB and MongoDB C++ Interface
External Websites
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
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)