insert

Insert one or multiple documents into MongoDB collection

Description

example

n = insert(conn,collection,documents) returns the number of documents inserted into a collection using the MongoDB® connection. Specify one or multiple documents to insert.

Examples

collapse all

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

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)

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

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)

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

Create two documents using these workspace variables:

  • employee — Double array

  • department — Cell array of character vectors

  • salary — 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)

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

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)

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

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

collapse all

MongoDB connection, specified as a mongo object.

Collection name, specified as a string scalar.

Example: "taxidata"

Data Types: string

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 object

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

collapse all

Number of documents inserted into a collection in the database, returned as a numeric scalar.

Introduced in R2017b