主要内容

出口MATLAB数据转换成MongoDB

这个例子展示了如何从MATLAB中导出表和结构数据®工作区到新的MongoDB®使用MongoDB的数据库工具箱™接口收集。然后,该示例展示了如何计算集合中文档的数量、从集合中删除文档以及删除集合。

要运行此示例,必须首先安装MongoDB的Database Toolbox接口。有关详细信息,请参见MongoDB安装数据库工具箱接口

该示例使用了两个数据集:patients.xls,其中包含患者数据tsunamis.xlsx,其中包含海啸数据。的文件中可以找到这些数据集的文件工具箱/ matlab /演示文件夹中。

连接到MongoDB

创建到数据库的MongoDB连接mongotest.这里是数据库服务器dbtb01使用端口号托管该数据库27017

服务器=“dbtb01”;端口= 27017;dbname =“mongotest”;康涅狄格州= mongo(服务器、端口dbname)
conn = mongo属性:数据库:'mongotest'用户名:"服务器:{'dbtb01'}端口:27017 CollectionNames: {'airlinesmall', 'employee', 'largedata'…TotalDocuments: 23485919

康涅狄格州蒙戈对象,该对象包含MongoDB连接。对象属性包含有关连接和数据库的信息。

  • 数据库名称为mongotest

  • 用户名为空。

  • 数据库服务器为dbtb01

  • 端口号为27017

  • 该数据库包含六个文档集合。集合的前三个名称是airlinesmall员工,largedata

  • 该数据库包含23,485,919个文档。

检查MongoDB连接。

isopen(康涅狄格州)
符合逻辑的1

数据库连接成功isopen函数返回1.否则,数据库连接关闭。

创建集合并将数据导出到MongoDB

方法加载数据集readtable函数。方法将海啸数据转换为结构table2struct函数。MATLAB工作区包含patientdata表和tsunamidata结构。

patientdata = readtable (“patients.xls”);数据= readtable (“tsunamis.xlsx”);tsunamidata = table2struct(数据);

使用MongoDB连接创建集合来存储患者和海啸数据。

patientcoll =“病人”;tsunamicoll =“海啸”;createCollection(康涅狄格州patientcoll) createCollection(康涅狄格州,tsunamicoll)

将表数据导出到病人收集。n包含已插入文档的数量。

n =插入(康涅狄格州patientcoll patientdata)
n = 100

将结构数据导出到海啸收集。n包含已插入文档的数量。

n =插入(康涅狄格州tsunamicoll tsunamidata)
n = 162

清点托收单据

控件中所有集合的名称mongotest数据库。新集合病人而且海啸出现在字符向量的单元格数组中。

conn.CollectionNames”
ans = 9×1 cell array {'airlinesmall'} {'employee'} {'largedata'} {'nyctaxi'} {'patients'} {'product'} {'restaurants'} {'tsunamis'} {'updateCollection'}

计算两个新集合中的文档数量。

ntsunamis = count(conn,tsunamicoll)
npatient = 100 ntsunami = 162

删除文档和删除集合

从两个集合中删除所有文档。npatients而且ntsunamis包含从每个集合中删除的文档的数量。

patientcoll npatients =删除(康涅狄格州,“{}”) ntsunamis = remove(conn,tsunamicoll)“{}”
npatient = 100 ntsunami = 162

对象中删除两个集合mongotest数据库。

dropCollection(康涅狄格州patientcoll) dropCollection(康涅狄格州,tsunamicoll)

关闭MongoDB连接

关上(康涅狄格州)

另请参阅

|||||||

相关的话题

外部网站