Main Content

Import and ExportMATLABObjects UsingMongoDB

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 theTensileDataclass. 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 Objects

Create theTensileDataobjectstdcsfor carbon steel materials andtdssfor 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]);

Connect toMongoDB

Create a MongoDB connection to the databasemongotest. Here, the database serverdbtb01hosts 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

connis themongoobject 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 theisopenfunction returns1. Otherwise, the database connection is closed.

Create Collection inMongoDB

Create theTensileDatacollection using the MongoDB connection.

collection ="TensileData"; createCollection(conn,collection)

Export Objects intoMongoDB

Export theTensileDataobjects into the collection. Theinsertfunction serializes theTensileDataobjects into a JSON-style structure.ntdcsandntdsscontain the number of objects exported into the collection.

ntdcs = insert(conn,collection,tdcs); ntdss = insert(conn,collection,tdss);

Import Objects intoMATLABWorkspace

Import theTensileDataobjects into the MATLAB workspace. Thefindfunction deserializes theTensileDataobjects into thedocumentsstructure 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 Documents and Drop Collection

Remove all documents from the collection.ncontains the number of documents removed from the collection.

n = remove(conn,collection,'{}')
n = 2

Drop the collection.

dropCollection(康涅狄格州、收集)

CloseMongoDBConnection

close(conn)

See Also

|||||||

Related Topics

External Websites