This example shows how to export objects from the MATLAB®workspace into MongoDB®using the Database Toolbox™ interface for MongoDB. The export serializes the objects in MongoDB. Then, the example shows how to import objects back into the MATLAB workspace. The import deserializes the objects and recreates them in MATLAB for method execution. After the export and import, the example shows how to drop the collection.
To run this example, you must first install the Database Toolbox interface for MongoDB. For details, seeDatabase Toolbox Interface for MongoDB Installation.
In this example, the objects belong to theTensileData
class. This class is a sample class in MATLAB. The data used to create the objects is sample data. For details, seeRepresenting Structured Data with Classes. To run the code in this example, define the class in the current folder.
The data represents tensile stress or strain measurements. To calculate the elastic modulus of various materials, use this data. In simple terms, stress is the force applied to a material, and strain is the resulting deformation. The ratio of stress to strain defines a characteristic of the material.
Create theTensileData
objectstdcs
for carbon steel materials andtdss
for stainless steel materials.
tdcs = TensileData('carbon steel',1,...[2e4 4e4 6e4 8e4],[.12 .20 .31 .40]); tdss = TensileData('stainless steel',1,...[2e4 4e4 6e4 8e4],[.06 .10 .16 .20]);
Create a MongoDB connection to the databasemongotest
. Here, the database serverdbtb01
hosts this database using port number27017
.
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 themongo
object that contains the MongoDB connection. The object properties contain information about the connection and the database.
The database name ismongotest
.
The user name is blank.
The database server isdbtb01
.
The port number is27017
.
This database contains six document collections. The first three collection names areairlinesmall
,employee
, andlargedata
.
This database contains 23,485,919 documents.
Verify the MongoDB connection.
isopen(conn)
ans = logical 1
The database connection is successful because theisopen
function returns1
. Otherwise, the database connection is closed.
Create theTensileData
collection using the MongoDB connection.
collection ="TensileData"; createCollection(conn,collection)
Export theTensileData
objects into the collection. Theinsert
function serializes theTensileData
objects into a JSON-style structure.ntdcs
andntdss
contain the number of objects exported into the collection.
ntdcs = insert(conn,collection,tdcs); ntdss = insert(conn,collection,tdss);
Import theTensileData
objects into the MATLAB workspace. Thefind
function deserializes theTensileData
objects into thedocuments
structure array.
documents = find(conn,collection);
Recreate the objects in the MATLAB workspace.
tdcs = TensileData(documents(1).Material,documents(1).SampleNumber,...documents(1).Stress,documents(1).Strain); tdss = TensileData(documents(2).Material,documents(2).SampleNumber,...documents(2).Stress,documents(2).Strain);
You can execute methods of the objects after they appear in the MATLAB workspace.
Remove all documents from the collection.n
contains the number of documents removed from the collection.
n = remove(conn,collection,'{}')
n = 2
Drop the collection.
dropCollection(康涅狄格州、收集)
close(conn)
mongo
|isopen
|close
|find
|createCollection
|dropCollection
|insert
|remove