vs2019 下用 vb.net编写窗体程序连接 mongodb4.2的方法
说起来,查看mongodb官方的接口文档是场噩梦,尽管mongodb官方花了大力气整顿了它的api,但是简单的接口罗列,0代码示范,让人无从开始。幸亏有很多天才,成功破译,我才得以沿着他们走的路,照猫画虎的走下去。整个项目结构如下:
类文件中vbtest.vb数据库实体类对应着mongodb文档vbtest,用于数据操作测试
imports mongodb.bson public class vbtest public _id as objectid public content as string end class
(optional)vbmongo.vb是绑定好数据库实体类vbtest的版本
imports mongodb.bson imports mongodb.driver public class vbmongo public client public collection as imongocollection(of vbtest) public database as imongodatabase public mlist as list(of vbtest) public builder as filterdefinitionbuilder(of vbtest) = new filterdefinitionbuilder(of vbtest) public filter as filterdefinition(of vbtest) public sub new(byval dbname as string, byval collectionname as string) client = new mongoclient("mongodb://127.0.0.1:27017") database = client.getdatabase(dbname) collection = database.getcollection(of vbtest)(collectionname) end sub public async function loadrecords() as task(of list(of vbtest)) filter = builder.ne(of objectid)("_id", new objectid())//_id不等于空的记录,通过这种方法蹩脚的实现了查找全部的功能builder的大多数条件设置函数都用到了泛型ne(of tfield) dim mlist as list(of vbtest) = await collection.find(filter).tolistasync()//tfield可以理解为 type of field 对应的是实体类中字段的数据类型例如 objectid或者string等等 return mlist end function public async function loadone(byval _id as string) as task(of vbtest) filter = builder.eq(of objectid)("_id", new objectid(_id)) dim mlist as vbtest = await collection.find(filter).firstordefaultasync() return mlist end function public async function updateone(byval _id as string, byval doc as vbtest) as task(of vbtest) filter = builder.eq(of objectid)("_id", new objectid(_id)) dim up as updatedefinitionbuilder(of vbtest) = new updatedefinitionbuilder(of vbtest)() dim updef as updatedefinition(of vbtest) updef = up.set(of string)("content", doc.content) '.set(of string)("content", doc) collection.updateoneasync(filter, updef) end function end class
说起来mongodb的连接可以分几步,根据连接字符串打开链接,获取数据库,获取文档,进而在文档的基础上进行增删改查。对于vb.net操作mongodb,完全是根据c#的操作范例,根据vb.net的语法特点临摹出来的。
首先需要对项目进行nuget包的安装,我们要搜寻的是mongodb.driver
这里创建了一个vb.net的泛型类vbmongot,只写了单个查找,查找列表和更新操作,函数中大量成对出现了async和await关键字,用于标识该功能使用了异步编程,在窗体程序中进行调用的时候,仍需要在成对的使用async await的关键字,因为异步函数返回的往往是task<tresult>类型(c#)或者 task(of tresult) (vb.net)需要在调用函数体中使用await关键字拿到最终结果imports mongodb.bsonimports mongodb.driver
public class vbmongot(of t) public client public collection as imongocollection(of t) public database as imongodatabase public mlist as list(of t) public builder as filterdefinitionbuilder(of t) = new filterdefinitionbuilder(of t) public up as updatedefinitionbuilder(of t) public updef as updatedefinition(of t) public filter as filterdefinition(of t) public sub new(byval dbname as string, byval collectionname as string) client = new mongoclient("mongodb://127.0.0.1:27017") database = client.getdatabase(dbname) collection = database.getcollection(of t)(collectionname) end sub public async function loadrecords() as task(of list(of t)) filter = builder.ne(of objectid)("_id", new objectid()) dim mlist as list(of t) = await collection.find(filter).tolistasync() return mlist end function public async function loadone(byval _id as string) as task(of t) filter = builder.eq(of objectid)("_id", new objectid(_id))//_id满足查找条件的记录,builder的大多数条件设置函数都用到了泛型:例如ne(of tfield) dim mlist as t = await collection.find(filter).firstordefaultasync() return mlist end function public async function updateone(byval filter as filterdefinition(of t), byval updef as updatedefinition(of t)) as task(of t) 'updef = up.set(of string)("content", doc.content) '.set(of string)("content", doc) await collection.updateoneasync(filter, updef) end function end class
在mongodb这一版本的官方api里,imongocollection对象是查找,添加,更新,删除等动作的发起者,这里面查找,更新,删除都要用到查询条件,
官方谓之filter(过滤器)c#: filterdefinition<t> vb.net:filterdefinition (of t)
而过滤器是过滤器模具的建模结果
过滤器模具:c#: filterdefinitionbuilder<t> vb.net:filterdefinitionbuilder (of t)
在mongodb.driver这个大类下面 建议直接翻看filterdefinitionbuilder的介绍 https://mongodb.github.io/mongo-csharp-driver/2.10/apidocs/html/t_mongodb_driver_filterdefinitionbuilder_1.htm
同样的数据库文档的更新操作由updatedefinitionbuilder 设置更新字段后产生的 updatedefinition完成
窗体程序部分
imports mongodb.bson imports mongodb.driver public class form1 public client public bsdoc as vbtest public collection as imongocollection(of vbtest) public database as imongodatabase public mlist as list(of vbtest) public builder as filterdefinitionbuilder(of vbtest) = new filterdefinitionbuilder(of vbtest) public vm as vbmongot(of vbtest) = new vbmongot(of vbtest)("meandmycoach", "vbtest") public filter as filterdefinition(of vbtest) private async sub button1_click(sender as object, e as eventargs) handles button1.click bsdoc.content = richtextbox1.text vm.filter = vm.builder.eq(of objectid)("_id", new objectid(combobox1.text)) vm.up = new updatedefinitionbuilder(of vbtest) vm.updef = vm.up.set(of string)("content", bsdoc.content) 'vm.up.set(of string)("content", bsdoc.content) vm.updef = vm.up.combine(vm.updef) vm.updateone(vm.filter, vm.updef) end sub private sub form1_load(sender as object, e as eventargs) handles mybase.load vm = new vbmongot(of vbtest)("meandmycoach", "vbtest") dim bsdoc as vbtest = new vbtest() end sub private async sub picturebox1_click(sender as object, e as eventargs) handles picturebox1.click mlist = await vm.loadrecords() for i as integer = 0 to mlist.count - 1 combobox1.items.add(mlist(i)._id.tostring()) next combobox1.text = combobox1.items(0) label2.text = mlist.count.tostring() end sub private sub richtextbox1_textchanged(sender as object, e as eventargs) handles richtextbox1.textchanged end sub private async sub combobox1_textchanged(sender as object, e as eventargs) handles combobox1.textchanged bsdoc = await vm.loadone(combobox1.text) richtextbox1.text = bsdoc.content end sub private async sub combobox1_selectedindexchanged(sender as object, e as eventargs) handles combobox1.selectedindexchanged end sub end class
功能描述:通过点击图片,加载所有的vbtest文档记录,combobox1专门用于显示objectid,可以通过tostring()方法转换为普通字符串
随着下拉框objectid的改变,richtext1控件加载vbtest实体类对应记录的content字段,可以更改字段内容后点击更新提交按钮完成文档的updateoneasync操作
总结
到此这篇关于vs2019 下用 vb.net编写窗体程序连接 mongodb4.2的文章就介绍到这了,更多相关vs2019连接 mongodb4.2内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!