sii6n018 Using MongoD8 on Windows platform with VB.NET
consulity
Using MongoDB on Windows platform with VB.NET
5 3K - ° a i
Abstract
‘This article describes the steps to install MongoDB on Windows platform, Furthermore, it shows
some basic examples accessing a database using VB.NET and the MongoDB - .NET client library.
According to this source, MongoDB is ranked #4 on the list of most popular databases, while
CouchDB (another NoSQL database) is ranked at #23.
Some good examples for VB.NET can be found here
Objective
Install MongoDB, create a sample database and perform CRUD operations.
Process
[1] Download MongoDB from here
[2] Install MongoDB completely
[3] Create a path for MongoDB databases €:\mongo\db\ and for logs C:\mongo\logs\
[4] Create a config file C:\mongo\mongod.conf with the following content:
dbpath = c:/mongo/db
Logpath = c:/nongo/logs/mongo. log
[5] Install MongoDB as Service with the config file created before:
Open a cmd window as administrator (e.g, C:\Program Files\MongoDB\Server\3.2\bin) and run
mongod --config
\mongo\nongod.coné" -install)
Run services. msc, refresh and check if service is running properly
[6] Create a new .NET project for Visual Basic (e.g. a windows form app)
[7] Install .NET driver for MongoDB via Visual Studio Package Manager
Install-Package mongocsharpdriver
(requires NET Framework 4.52)
hitpsiconsuliy:neicontenUconsully-nstalongoD 8. aspx ussii6n018 Using MongoD8 on Windows platform with VB.NET
For new projects, the mongocsharpdriver should not be used (cf. here)
Instead, install the MongoDB Driver and Bson library via NuGet package manager in Visual Studio:
Install-Package Mongod8.Driver
Install-Package MongoDB.Driver.Core
Install-Package MongoDB.3son
[8] Perform CRUD operations with the sample code given below
Optional steps
[9] Download DB Management UI Robomongo from https://robomongo.org/download
[10] Download UI client from https://github.com/rsercano/mongoclient
Sample Code (VB.NET) - *legacy*
[1] DB CONNECTION
Imports MongoDB.Bson
Imports MongoDB.Driver
Dim server As MongoServer
Dim client As MongoClient
Din db As MongoDatabase
clients New MongoClient ("mongodb: //1ocalhost/")
db = client.GetServer().GetDatabase("test")
Note: Ifa the requested database does not exist it will automatically be created the first time you add
acollection.
[2] CREATE
Dim collection As MongoCollection= db.GetCollection(0# BsonDocument)("enployees")
Dim emp As Bsondocunent
With emp
job_desc",
:Add("“location", “hks-hq”)
End With
collection. Insert(enp)
[3] READ
D
Dim query = New QueryDocument("nane", "Neunann")
For Each item As BsonDocument In collection.Find(query)
Dim name As BsonElenent = item.GetElenent("nane")
Console.WriteLine("Name: {@}", nane.Value)
Next,
hitpsiconsuly:neicantenUconsully-nstalongoD8. aspx 28sii6n018 Using MongoDB on Windows platform with VB.NET
[4] UPDATE
“UPDATE
Dim gs = New QueryDocument("_id", 23)
Dim found As BsonDocument= collection.Find(qs).First
* console. ind .GetElement ("Location"). Value. ToString)
found. Set( "Hei ligkreuzsteinach")
collection.Save( found)
[5] DELETE,
“DELETE
Din q = New QueryDocunent("_id", 24)
collection. Rerove(q)
[6] REMOVE DB
“REMOVE 0B:
Din rdb As MongoDatabase
rdb = client.GetServer().GetDatabase("al*")
rdb.Drop()
Sample Code (VB.NET) - *new*
[1] DB CONNECTION
Imports MongodB.Bson
“Imports MongoDB.Driver
Dim client As Mongoclient
Dim de As IMongoatabase
clients New MongoClient ("nongodb: //Localhost/")
db = client.GetDatabase("db")
[2] CREATE
Din collection As TMongoCollection(0f BsonDocument) = db.GetCollection(oF
Bsondocunent ("employees")
“CREATE
Dim emp As BsonDocunent = New BsonDocument
mp
"id", 24) "Guid.NewGuid().Tostring)
‘Nane", "Neunann”)
'first_name", "Dieter"
Job_desc", "Developer
location", “hks-ha")
End With
collection. Insertone(enp)
hntpsiconsuliy:neicantentconsully-nstalongoD8. aspx a8sii6n018 Using MongoD8 on Windows platform with VB.NET
Din filter = Builders (ofBsonDocument) .Filter.£q(Of String)("name", "Neumann”)
For Each item As BsonDocunent In collection.Find(filter).ToList
Dim name As BsonElenent = item.GetElenent ("mane")
Console.writeLine(*Nane: (@}", nane.Value)
Next
[4] UPDATE
“UPDATE
Din fltr = Builders(OfBsonDocument).Filter.£q(OF Long)("_id", 24)
collection.UpdateOne(fitr,New BsonDocument("Sset", New BsonDocurent( "location",
“Wei ligkreuzsteinach")))
“many records
Din # = Builders (Of @sonDocunent).Filter.£q(Of String)("name", "Neumann”)
collection.UpdateMany(f, New BsonDocunent("$set", New BsonDocument("location",
"HKS")))
DELETE
“DELETE
Din q = Builders (of BsonDocunent) .Filter.Eq(Of Long)("_id", 22)
collection.Deletedne(q)
"DELETE COLLECTION
db DropCollection(
[5] REMOVE DB
REMOVE DB
client .Dropbatabase(“db1")
Sample Code Collection
Find some typical functions here:
Private Function UpdateUserData(ByVal ud
Asconsulity_Office365NailNotification_Userbata) As String
Dim db As TMongobatabase = getConnection()
Dim col As IMongocollection(OF consulity Office26SMailNotification_Userbata)
col = db.Getcollection(of consulity_office365MaiINotification_UserData)
(Coffices6smailNoti fication")
Dim fltr= Builders (0fconsulity_OfFices65Mai INotification_UserData).Filter.Fq(OF
string)(*uId", ud.UId)
‘Din itm As Newconsulity_Office365MailNotification_UserData
col.Updateltany(Fltr, New BsonDocument("$set", NewisonDocument("value”,
strNewalue)))
hitpsiconsuly:neicantenUconsully-nstalongoD8. aspx 46sii6n018 Using MongoDB on Windows platform with VB.NET
"Dim upd = Builders(Of consulity_OFFice365Mai Notification UserData).Update
"Dim updd = upd.Set(OF String) ("LastEmailid” ud. LastémailIa) .Set(OF Date)
("LastUpdated”, DateAndTime .Now)
‘col.Updateone(fltr, updd)
* updatetinestanp
ud. Lastupdated =DateandTine.Now
col.-Replacedne(fltr,ud)
Return “updated record for: “ & ud.UId
End Function
Conclusion
The installation of MongoDB and the NET library ran without problems. The Robomongo Ul is a
great tool,
One step is unclear: Is MongoDB already available as service w/o running the install command via
‘command line?
Sources
[1] http://mongodb github 1o/mongo-csharp-driver/2.0/
[2] http://mongoosejs.com/
[3] http://mongodb github.io/mongo-csharp-driver/1.11 /ling/
[4] hutps://en.wikipedia.org/wiki/MongoDB
[5] Windows Install
[6] nuget
[7] MongoDB Connect to VB.NET Visual Studio 2013
{8} Connecting MongoDe with AS
[9] MongoDB Basies fo ple
[10] http://www. Sturpeomeecnn/U ploadFile/736ca4/crud-operation-using-mongodb/
[11] http://www.codeproject.com/Articles/858908/MongoDB-with-NET-Setu}
operations
[12] https://mongodb-documentation.readthedocs.io/en/latest/ecosystem/tutorial/use-ling-
queries-with-csharp-driverhhtml (1!)
[13] https://speakerdeck.com/mongodb/dot-net-driver-robert-stam-LOgen
[14] Official NET driver for MongoDB,
[15] Microsoft HTTP Client Libraries 2.2.29
[16] Json.NET 9.0.1
[17] consulity: MongoDB live example with VB.NET
Author: Dieter Neumann; 2016-07-14
hitpsiconsuliy:neicantenUconsully-nstalongoD 8. aspx 55