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.
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.
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"; createCollection(conn,patientcoll) createCollection(conn,tsunamicoll)
Export table data into the patients
collection.
n
contains the number of documents inserted.
n = insert(conn,patientcoll,patientdata)
n = 100
Export structure data into the tsunamis
collection.
n
contains the number of documents inserted.
n = insert(conn,tsunamicoll,tsunamidata)
n = 162
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.
conn.CollectionNames'
ans = 9×1 cell array {'airlinesmall' } {'employee' } {'largedata' } {'nyctaxi' } {'patients' } {'product' } {'restaurants' } {'tsunamis' } {'updateCollection'}
Count the number of documents in the two new collections.
npatients = count(conn,patientcoll) ntsunamis = count(conn,tsunamicoll)
npatients = 100 ntsunamis = 162
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 = 100 ntsunamis = 162
Drop both collections from the mongotest
database.
dropCollection(conn,patientcoll) dropCollection(conn,tsunamicoll)
close(conn)
close
| count
| createCollection
| dropCollection
| insert
| isopen
| mongo
| remove