说起来,查看Mongodb官方的接口文档是场噩梦,尽管mongodb官方花了大力气整顿了它的API,但是简单的接口罗列,0代码示范,让人无从开始。幸亏有很多天才,成功破译,我才得以沿着他们走的路,照猫画虎的走下去。整个项目结构如下:

vs2019 下用 vb.net编写窗体程序连接 mongodb4.2的方法

类文件中vbtest.vb数据库实体类对应着mongodb文档vbtest,用于数据操作测试

Imports MongoDB.Bson

Public Class vbtest
 Public _id As ObjectId
 Public content As String
End Class

vs2019 下用 vb.net编写窗体程序连接 mongodb4.2的方法

(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

vs2019 下用 vb.net编写窗体程序连接 mongodb4.2的方法

这里创建了一个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完成

窗体程序部分

vs2019 下用 vb.net编写窗体程序连接 mongodb4.2的方法

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连接,mongodb4.2,vs2019用,vb.net编写窗体程序

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金钱帮资源网 Copyright www.kbjia.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。