此示例显示如何从MATLAB导出对象®工作区进入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
班级。此类是Matlab中的示例类。用于创建对象的数据是示例数据。有关详细信息,请参阅表示具有类的结构化数据. 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
是个mongo
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
,员工
, 和largedata
.
该数据库包含23,485,919件文件。
Verify the MongoDB connection.
Isopen(Conn)
ans = logical 1
数据库连接成功,因为开了
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. The插入
function serializes theTensileData
objects into a JSON-style structure.ntdcs
和ntdss
包含导出到集合中的对象数。
ntdcs = insert(conn,collection,tdcs); ntdss = insert(conn,collection,tdss);
Import theTensileData
objects into the MATLAB workspace. The找
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);
您可以在Matlab工作区中出现后执行对象的方法。
Remove all documents from the collection.n
包含从集合中删除的文档数。
n =删除(Conn,Collection,'{}')
n = 2
放下收藏。
dropCollection(康涅狄格州、收集)
close(conn)
mongo
|开了
|close
|找
|CreateCollection.
|dropCollection
|插入
|remove