Main Content

Export MATLAB Data into MongoDB

This example shows how to export table and structure data from the MATLAB® workspace into new MongoDB® collections using the Database Toolbox™ interface for MongoDB. The example then shows how to count the number of documents in the collections, remove documents from the collections, and drop the collections.

To run this example, you must first install the Database Toolbox interface for MongoDB. For details, see Database Toolbox Interface for MongoDB Installation.

The example uses two data sets: patients.xls, which contains patient data, and tsunamis.xlsx, which contains tsunami data. You can find files for these data sets in the toolbox/matlab/demos folder.

Connect to MongoDB

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.

ans =



The database connection is successful because the isopen function returns 1. Otherwise, the database connection is closed.

Create Collections and Export Data into MongoDB

Load the data sets using the readtable function. Convert tsunami data to a structure using the table2struct function. The MATLAB workspace contains the patientdata table and the tsunamidata structure.

patientdata = readtable('patients.xls'); 
data = readtable('tsunamis.xlsx');
tsunamidata = table2struct(data);

Create collections to store the patient and tsunami data using the MongoDB connection.

patientcoll = "patients";
tsunamicoll = "tsunamis";


Export table data into the patients collection. n contains the number of documents inserted.

n = insert(conn,patientcoll,patientdata)
n =


Export structure data into the tsunamis collection. n contains the number of documents inserted.

n = insert(conn,tsunamicoll,tsunamidata)
n =


Count Documents in Collections

Display the names of all the collections in the mongotest database. The new collections patients and tsunamis appear in the cell array of character vectors.

ans =

  9×1 cell array

    {'airlinesmall'    }
    {'employee'        }
    {'largedata'       }
    {'nyctaxi'         }
    {'patients'        }
    {'product'         }
    {'restaurants'     }
    {'tsunamis'        }

Count the number of documents in the two new collections.

npatients = count(conn,patientcoll)
ntsunamis = count(conn,tsunamicoll)
npatients =


ntsunamis =


Remove Documents and Drop Collections

Remove all documents from both collections. npatients and ntsunamis contain the number of documents removed from each collection.

npatients = remove(conn,patientcoll,'{}')
ntsunamis = remove(conn,tsunamicoll,'{}')
npatients =


ntsunamis =


Drop both collections from the mongotest database.


Close MongoDB Connection


See Also

| | | | | | |

Related Topics

External Websites