說(shuō)起來(lái),查看Mongodb官方的接口文檔是場(chǎng)噩夢(mèng),盡管mongodb官方花了大力氣整頓了它的API,但是簡(jiǎn)單的接口羅列,0代碼示范,讓人無(wú)從開始。幸虧有很多天才,成功破譯,我才得以沿著他們走的路,照貓畫虎的走下去。整個(gè)項(xiàng)目結(jié)構(gòu)如下:
類文件中vbtest.vb數(shù)據(jù)庫(kù)實(shí)體類對(duì)應(yīng)著mongodb文檔vbtest,用于數(shù)據(jù)操作測(cè)試
Imports MongoDB.Bson
Public Class vbtest
Public _id As ObjectId
Public content As String
End Class
(optional)vbmongo.vb是綁定好數(shù)據(jù)庫(kù)實(shí)體類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不等于空的記錄,通過(guò)這種方法蹩腳的實(shí)現(xiàn)了查找全部的功能Builder的大多數(shù)條件設(shè)置函數(shù)都用到了泛型Ne(Of TField)
Dim mlist As List(Of vbtest) = Await collection.Find(filter).ToListAsync()//TField可以理解為 type of field 對(duì)應(yīng)的是實(shí)體類中字段的數(shù)據(jù)類型例如 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
說(shuō)起來(lái)mongodb的連接可以分幾步,根據(jù)連接字符串打開鏈接,獲取數(shù)據(jù)庫(kù),獲取文檔,進(jìn)而在文檔的基礎(chǔ)上進(jìn)行增刪改查。對(duì)于vb.net操作mongodb,完全是根據(jù)c#的操作范例,根據(jù)vb.net的語(yǔ)法特點(diǎn)臨摹出來(lái)的。
首先需要對(duì)項(xiàng)目進(jìn)行Nuget包的安裝,我們要搜尋的是MongoDB.Driver
這里創(chuàng)建了一個(gè)vb.net的泛型類vbmongoT,只寫了單個(gè)查找,查找列表和更新操作,函數(shù)中大量成對(duì)出現(xiàn)了Async和Await關(guān)鍵字,用于標(biāo)識(shí)該功能使用了異步編程,在窗體程序中進(jìn)行調(diào)用的時(shí)候,仍需要在成對(duì)的使用Async Await的關(guān)鍵字,因?yàn)楫惒胶瘮?shù)返回的往往是TaskTResult>類型(c#)或者 Task(Of TResult) (vb.net)需要在調(diào)用函數(shù)體中使用await關(guān)鍵字拿到最終結(jié)果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的大多數(shù)條件設(shè)置函數(shù)都用到了泛型:例如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對(duì)象是查找,添加,更新,刪除等動(dòng)作的發(fā)起者,這里面查找,更新,刪除都要用到查詢條件,
官方謂之filter(過(guò)濾器)C#: FilterDefinitionT> VB.net:FilterDefinition (Of T)
而過(guò)濾器是過(guò)濾器模具的建模結(jié)果
過(guò)濾器模具:C#: FilterDefinitionBuilderT> VB.net:FilterDefinitionBuilder (Of T)
在Mongodb.Driver這個(gè)大類下面 建議直接翻看FilterDefinitionBuilder的介紹 https://mongodb.github.io/mongo-csharp-driver/2.10/apidocs/html/T_MongoDB_Driver_FilterDefinitionBuilder_1.htm
同樣的數(shù)據(jù)庫(kù)文檔的更新操作由UpdateDefinitionBuilder 設(shè)置更新字段后產(chǎn)生的 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
功能描述:通過(guò)點(diǎn)擊圖片,加載所有的vbtest文檔記錄,combobox1專門用于顯示ObjectId,可以通過(guò)ToString()方法轉(zhuǎn)換為普通字符串
隨著下拉框ObjectId的改變,richtext1控件加載vbtest實(shí)體類對(duì)應(yīng)記錄的Content字段,可以更改字段內(nèi)容后點(diǎn)擊更新提交按鈕完成文檔的UpdateOneAsync操作
總結(jié)
到此這篇關(guān)于vs2019 下用 vb.net編寫窗體程序連接 mongodb4.2的文章就介紹到這了,更多相關(guān)vs2019連接 mongodb4.2內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- MongoDB 用戶管理
- Linux安裝MongoDB啟動(dòng)及常見問(wèn)題解決
- MongoDB如何查看版本信息詳解
- Centos7 yum安裝mongodb實(shí)現(xiàn)步驟詳解
- mongodb 4.0副本集搭建的全過(guò)程
- .Net Core使用MongoDB的詳細(xì)教程
- 開源 5 款超好用的數(shù)據(jù)庫(kù) GUI 帶你玩轉(zhuǎn) MongoDB、Redis、SQL 數(shù)據(jù)庫(kù)(推薦)
- MongoDb CPU利用率過(guò)高問(wèn)題如何解決