这个例子展示了如何从MATLAB中导出表和结构数据®新的MongoDB工作区®使用MongoDB的Database Toolbox™接口的集合。然后,示例显示了如何计算集合中的文档数,从集合中删除文档,然后删除集合。
要运行这个示例,您必须首先为MongoDB安装数据库工具箱接口。有关详细信息,请参见安装MongoDB的数据库工具箱接口.
该示例使用了两个数据集:patients.xls
,其中包含患者数据tsunamis.xlsx
,其中包含海啸数据。中可以找到这些数据集的文件工具箱/ matlab /演示
文件夹中。
创建到数据库的MongoDB连接mongotest
.这里是数据库服务器dbtb01
使用端口号托管该数据库27017
.
服务器=“dbtb01”;端口= 27017;dbname =“mongotest”;康涅狄格州= mongo(服务器、端口dbname)
conn = mongo with properties: Database: 'mongotest' UserName: " Server: {'dbtb01'} Port: 27017 CollectionNames: {' airlinesal ', 'employee', 'largedata'…and 3 more} TotalDocuments: 23485919
康涅狄格州
是个蒙戈
包含MongoDB连接的对象。对象属性包含有关连接和数据库的信息。
数据库名称为mongotest
.
用户名为空。
数据库服务器为dbtb01
.
端口号为27017
.
这个数据库包含六个文档集合。前三个集合名是airlinesmall
,员工
, 和largedata
.
该数据库包含23,485,919件文件。
检查MongoDB连接。
Isopen(Conn)
逻辑1
数据库连接成功,因为开了
函数返回1
.否则,数据库连接将关闭。
控件加载数据集readtable
函数。将海啸数据转换为结构使用table2struct
函数。matlab工作区包含patientdata
表和tsunamidata
结构。
patientdata = readtable (“patients.xls”);数据= readtable ('tsunamis.xlsx');tsunamidata = table2struct(数据);
使用MongoDB连接创建用于存储患者和海啸数据的集合。
患者=“病人”;tsunamicoll =“海啸”;createCollection(康涅狄格州patientcoll) createCollection(康涅狄格州,tsunamicoll)
将表数据导出到耐心
收藏。n
包含插入的文档数量。
n =插入(Conn,Patientcoll,患者Data)
n = 100.
将结构数据导出到海啸
收藏。n
包含插入的文档数量。
n =插入(康涅狄格州tsunamicoll tsunamidata)
n = 162
显示所有集合的名称mongotest
数据库。新集合耐心
和海啸
出现在字符向量的单元格数组中。
conn.CollectionNames”
ans = 9×1 cell array {' airlinsmall '} {'employee'} {'largedata'} {'nyctaxi'} {'patients'} {'product'} {'restaurants'} {'tsunamis'} {'updateCollection'}
计算两个新集合中文档的数量。
npatients = count(conn,patientcoll)ntsunamis = count(conn,tsunamicoll)
n病人= 100 n海啸= 162
从两个集合中删除所有文档。npatients
和ntsunamis
包含从每个集合中删除的文档数。
patientcoll npatients =删除(康涅狄格州,“{}”n .海啸,海啸,海啸“{}”)
n病人= 100 n海啸= 162
从mongotest
数据库。
DropCollection(Conn,Patiencoll)DropCollection(Conn,Tunamicoll)
关上(康涅狄格州)
蒙戈
|开了
|关闭
|CreateCollection.
|dropCollection
|数
|插入
|删除