出口MATLAB数据转换成MongoDB
这个例子展示了如何从MATLAB中导出表和结构数据®工作区到新的MongoDB®集合使用数据库工具箱™接口MongoDB。然后,该示例展示了如何计算集合中的文档数量、从集合中删除文档以及删除集合。
要运行此示例,必须首先安装MongoDB的Database Toolbox接口。详细信息请参见MongoDB安装数据库工具箱接口.
这个例子使用了两个数据集:patients.xls
,其中包含患者数据,以及tsunamis.xlsx
,其中包含海啸数据。的文件中可以找到这些数据集的文件工具箱/ matlab /演示
文件夹中。
连接到MongoDB
创建到数据库的MongoDB连接mongotest
.这里是数据库服务器dbtb01
使用端口号承载此数据库27017
.
服务器=“dbtb01”;端口= 27017;dbname =“mongotest”;Conn = mongo(服务器,端口,dbname)
conn = mongo与属性:数据库:'mongotest'用户名:"服务器:{'dbtb01'}端口:27017 CollectionNames: {'airlinesmall', 'employee', 'largedata'…和3更多}TotalDocuments: 23485919
康涅狄格州
是蒙戈
包含MongoDB连接的对象。对象属性包含有关连接和数据库的信息。
数据库名称为
mongotest
.用户名为空。
数据库服务器为
dbtb01
.端口号为
27017
.这个数据库包含六个文档集合。前三个集合名称为
airlinesmall
,员工
,largedata
.该数据库包含23,485,919个文档。
检查MongoDB连接。
isopen(康涅狄格州)
Ans =逻辑1
数据库连接成功,因为isopen
函数返回1
.否则,数据库连接关闭。
创建集合并将数据导出到MongoDB
方法加载数据集readtable
函数。方法将海啸数据转换为结构table2struct
函数。MATLAB工作区包含patientdata
表和tsunamidata
结构。
Patientdata = readtable(“patients.xls”);数据=可读数据(“tsunamis.xlsx”);Tsunamidata = table2struct(data);
使用MongoDB连接创建集合来存储患者和海啸数据。
patientcoll =“病人”;tsunamicoll =“海啸”;createCollection(康涅狄格州patientcoll) createCollection(康涅狄格州,tsunamicoll)
将表数据导出到病人
收集。n
包含已插入文档的数量。
N = insert(conn,patientcoll,patientdata)
N = 100
将结构数据导出到海啸
收集。n
包含已插入文档的数量。
N =插入(conn,tsunamicoll,tsunamidata)
N = 162
清点托收单据
控件中所有集合的名称mongotest
数据库。新系列病人
而且海啸
出现在字符向量的单元格数组中。
conn.CollectionNames”
ans = 9×1单元格数组{'airlinesmall'} {'employee'} {'largedata'} {'nyctaxi'} {'patients'} {'product'} {'restaurants'} {'tsunami '} {'updateCollection'}
计算两个新集合中的文档数量。
ntsunamis = count(conn,tsunamicoll)
n病人= 100 n海啸= 162
删除文档和删除集合
从两个集合中删除所有文档。npatients
而且ntsunamis
包含从每个集合中删除的文档数量。
Npatients = remove(conn,patientcoll,“{}”) ntsunamis = remove(conn,tsunamicoll,“{}”)
n病人= 100 n海啸= 162
中删除两个集合mongotest
数据库。
dropCollection(康涅狄格州patientcoll) dropCollection(康涅狄格州,tsunamicoll)
关闭MongoDB连接
关上(康涅狄格州)
另请参阅
蒙戈
|isopen
|关闭
|createCollection
|dropCollection
|数
|插入
|删除