Main Content

insert

Insert one or multiple documents into MongoDB collection

Since R2021b

Description

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

example

Examples

collapse all

Connect to MongoDB® using the MongoDB C++ interface and export one document from MATLAB® and insert it into a collection. Specify the document to insert as a structure. In this example, the collection represents employee data.

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.

Create one document as the document structure with three fields. Set the employee_id field to 28, department_id field to 80, and salary field to 200,000.

document.employee_id = 28;
document.department_id = 80;
document.salary = 200000;

Specify the employees collection. Insert the document into the collection by using the MongoDB C++ interface connection. The insert function inserts one document into the collection.

collection = "employees";
n = insert(conn,collection,document)
n = int64
    1

Close the MongoDB connection.

close(conn)

Connect to MongoDB® using the MongoDB C++ interface and export multiple documents from MATLAB® and insert them into a collection. Specify documents to insert as a structure array. In this example, the collection represents employee data.

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.

Create two documents as structures with these fields: employee_id, department_id, and salary. For the employee1 structure, set the employee_id field to 26, department_id field to 80, and salary field to 100,000. For the employee2 structure, set the same fields to the values 27, 90, and 150,000 respectively. Create the documents structure array from these documents.

employee1.employee_id = 26;
employee1.department_id = 80;
employee1.salary = 100000;

employee2.employee_id = 27;
employee2.department_id = 90;
employee2.salary = 150000;

documents = [employee1 employee2];

Specify the employees collection. Insert documents into the collection using the MongoDB connection. The insert function inserts two documents into the collection.

collection = "employees";
n = insert(conn,collection,documents)
n = int64
    2

Close the MongoDB connection.

close(conn)

Connect to MongoDB® using the MongoDB C++ interface and export documents from MATLAB® and insert them into a collection. Specify documents to insert as a table. In this example, the collection represents employee data.

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.

Create two documents using these workspace variables:

  • department_ids — Double array

  • employee_ids — Double array

  • salaries — Double array

Create the documents table from these workspace variables.

department_ids = [80;90];
employee_ids = [24;25];
salaries = [100000;150000];
documents = table(department_ids,employee_ids,salaries);

Specify the employees collection. Insert documents into the collection using the MongoDB connection. The insert function inserts two documents into the collection.

collection = "employees";
n = insert(conn,collection,documents)
n = int64
    2

Close the MongoDB connection.

close(conn)

Connect to MongoDB® using the MongoDB C++ interface and export documents from MATLAB® and insert them into a collection. Specify documents to insert as a cell array of structures. In this example, the collection represents employee data.

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.

Create two documents as the structures employee1 and employee2. Create the documents cell array using these structures.

employee1.department_id = 90;
employee1.employee_id = 22;
employee1.salary = 100000;

employee2.department_id = 80;
employee2.employee_id = 23;
employee2.salary = 150000;

documents = {employee1;employee2};

Specify the employees collection. Insert documents into the collection using the MongoDB C++ interface connection. The insert function inserts two documents into the collection.

collection = "employees";
n = insert(conn,collection,documents)
n = int64
    2

Close the MongoDB connection.

close(conn)

Input Arguments

collapse all

MongoDB C++ interface connection, specified as a connection 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

  • 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 an int64 scalar.

Note

The insert function does not return the containers.Map data type.

Version History

Introduced in R2021b