You are on page 1of 5
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 us sii6n018 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 28 sii6n018 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 a8

You might also like