insert
Insert one or multiple documents into MongoDB collection
The insert
function will be removed in a future release. Use the
insert
function of the MongoDB® C++ interface instead.
Description
returns the number of documents inserted into a collection using the MongoDB connection. Specify one or multiple documents to insert.n
= insert(conn
,collection
,documents
)
Examples
Insert One Document into Collection as Structure
Connect to MongoDB and export one document from MATLAB® and insert it into a collection. Specify the document to insert as a structure. Here, the collection represents employee data.
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.
Create one document as the document
structure with
these fields: employee
, department
,
and salary
.
document.employee = 28;
document.department = 'Sales';
document.salary = 200000;
Specify the employee
collection. Insert the document
into the collection by using the MongoDB connection. The insert
function inserts
one document into the collection.
collection = "employee";
n = insert(conn,collection,document)
n = 1
Close the MongoDB connection.
close(conn)
Insert Multiple Documents into Collection as Structure Array
Connect to MongoDB and export multiple documents from MATLAB and insert them into a collection. Specify documents to insert as a structure array. Here, the collection represents employee data.
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.
Create two documents as structures with these fields:
employee
, department
, and
salary
. Create the documents
structure array from these documents.
employee1.employee = 26; employee1.department = 'Sales'; employee1.salary = 100000; employee2.employee = 27; employee2.department = 'Training'; employee2.salary = 150000; documents = [employee1 employee2];
Specify the employee
collection. Insert documents into
the collection using the MongoDB connection. The insert
function inserts
two documents into the collection.
collection = "employee";
n = insert(conn,collection,documents)
n = 2
Close the MongoDB connection.
close(conn)
Insert Multiple Documents into Collection as Table
Connect to MongoDB and export documents from MATLAB and insert them into a collection. Specify documents to insert as a table. Here, the collection represents employee data.
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.
Create two documents using these workspace variables:
employee
— Double arraydepartment
— Cell array of character vectorssalary
— Double array
Create the documents
table from these
workspace variables.
employee = [26;27]; department = {'Sales';'Training'}; salary = [100000;150000]; documents = table(department,employee,salary);
Specify the employee
collection. Insert documents into
the collection using the MongoDB connection. The insert
function inserts
two documents into the collection.
collection = "employee";
n = insert(conn,collection,documents)
n = 2
Close the MongoDB connection.
close(conn)
Insert Multiple Documents into Collection as Cell Array of Structures
Connect to MongoDB and export documents from MATLAB and insert them into a collection. Specify documents to insert as a cell array of structures. Here, the collection represents employee data.
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.
Create two documents as the structures employee1
and
employee2
. Create the documents
cell array using these structures.
employee1.department = 'Sales'; employee1.employee = 26; employee1.salary = 100000; employee2.department = 'Training'; employee2.employee = 27; employee2.salary = 150000; documents = {employee1;employee2};
Specify the employee
collection. Insert documents into
the collection using the MongoDB connection. The insert
function inserts
two documents into the collection.
collection = "employee";
n = insert(conn,collection,documents)
n = 2
Close the MongoDB connection.
close(conn)
Insert Map
Object into Collection
Connect to MongoDB and export a Map
object from MATLAB and insert it into a collection. Here, the collection represents
employee data.
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.
Construct a Map
object document
that
contains an employee's payroll data for the first three months of the
year.
months = {'January','February','March'}; payslips = [4500,5000,4500]; document = containers.Map(months,payslips);
Specify the employee
collection. Insert the
Map
object into the collection using the MongoDB connection. The insert
function inserts
one document into the collection.
collection = "employee";
n = insert(conn,collection,document)
n = 1
Close the MongoDB connection.
close(conn)
Input Arguments
conn
— MongoDB connection
mongo
object
MongoDB connection, specified as a mongo
object.
collection
— Collection name
string scalar
Collection name, specified as a string scalar.
Example: "taxidata"
Data Types: string
documents
— Documents to insert
string scalar | character vector | structure | ...
Documents to insert into a MongoDB collection, specified as one of these types:
String scalar
Character vector
Structure
Structure array
Cell array of structures
Table
Map
objectHandle or value classes
When working with string scalars and character vectors, you specify key-value pairs as shown in these examples.
String scalar —
"{'department':'Sales','employeename':'George Mason'}"
Character vector —
'{''department'':''Sales'',''employeename'':''George Mason''}'
For handle and value classes, you can define your own class. After you instantiate a class, you can insert the resulting object into MongoDB. However, the resulting object properties must contain data types that can be converted to MATLAB data types. For example, if one of the object properties is a Java® object, then you cannot insert the object into MongoDB. For details about these classes, see Handle Classes.
Output Arguments
n
— Number of documents inserted
numeric scalar
Number of documents inserted into a collection in the database, returned as a numeric scalar.
Version History
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)