Using MongoDB on Windows platform with VB.NET

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 C:\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:/mongo/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 "c:\mongo\mongod.conf" -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.5?)
  8. 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 MongoDB.Driver
    Install-Package MongoDB.Driver.Core
    Install-Package MongoDB.Bson
  9. Perform CRUD operations with the sample code given below

  10. Optional steps

  11. Download DB Management UI Robomongo from https://robomongo.org/download
  12. 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
    Dim db As MongoDatabase

    client= New MongoClient("mongodb://localhost/")
    db = client.GetServer().GetDatabase("test")

    Note: If a the requested database does not exist it will automatically be created the first time you add a collection.

  2. CREATE
    Dim collection As MongoCollection= db.GetCollection(Of BsonDocument)("employees")

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'CREATE
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim emp As BsonDocument = New BsonDocument
    With emp
    .Add("_id", Guid.NewGuid().ToString)
    .Add("name", "Neumann")
    .Add("first_name", "Peter")
    .Add("job_desc", "Developer")
    .Add("location", "hks-hq")
    End With
    collection.Insert(emp)

  3. READ
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'READ
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim query = New QueryDocument("name", "Neumann")
    For Each item As BsonDocument In collection.Find(query)
    Dim name As BsonElement = item.GetElement("name")
    Console.WriteLine("Name: {0}", name.Value)
    Next
  4. UPDATE
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'UPDATE
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim qs = New QueryDocument("_id", 23)
    Dim found As BsonDocument= collection.Find(qs).First
    ' Console.WriteLine(found.GetElement("location").Value.ToString)
    found.Set("location", "Heiligkreuzsteinach")
    collection.Save(found)

  5. DELETE
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'DELETE
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim q = New QueryDocument("_id", 24)
    collection.Remove(q)
  6. REMOVE DB
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'REMOVE DB
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim rdb As MongoDatabase
    rdb = client.GetServer().GetDatabase("alf")
    rdb.Drop()


Sample Code (VB.NET) - *new*

  1. DB CONNECTION
    Imports MongoDB.Bson
    Imports MongoDB.Driver
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'CONNECTION
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim client As MongoClient
    Dim db As IMongoDatabase

    client= New MongoClient("mongodb://localhost/")
    db = client.GetDatabase("db")
  2. CREATE
    Dim collection As IMongoCollection(Of BsonDocument) = db.GetCollection(Of BsonDocument)("employees")

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'CREATE
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim emp As BsonDocument = New BsonDocument
    With emp
    .Add("_id", 24) 'Guid.NewGuid().ToString)
    .Add("name", "Neumann")
    .Add("first_name", "Dieter")
    .Add("job_desc", "Developer")
    .Add("location", "hks-hq")
    End With
    collection.InsertOne(emp)
  3. READ
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'READ
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim filter = Builders(OfBsonDocument).Filter.Eq(Of String)("name", "Neumann")
    For Each item As BsonDocument In collection.Find(filter).ToList
    Dim name As BsonElement = item.GetElement("name")
    Console.WriteLine("Name: {0}", name.Value)
    Next
  4. UPDATE
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'UPDATE
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim fltr = Builders(OfBsonDocument).Filter.Eq(Of Long)("_id", 24)
    collection.UpdateOne(fltr,New BsonDocument("$set", New BsonDocument("location", "Heiligkreuzsteinach")))

    'many records
    Dim f = Builders(Of BsonDocument).Filter.Eq(Of String)("name", "Neumann")
    collection.UpdateMany(f, New BsonDocument("$set", New BsonDocument("location", "HKS")))
    DELETE
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'DELETE
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Dim q = Builders(Of BsonDocument).Filter.Eq(Of Long)("_id", 22)
    collection.DeleteOne(q)
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'DELETE COLLECTION
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    db.DropCollection("employees")
  5. REMOVE DB
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' REMOVE DB
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    client.DropDatabase("db1")

Sample Code Collection

Find some typical functions here:

Private Function UpdateUserData(ByVal ud Asconsulity_Office365MailNotification_UserData) As String

    Dim db As IMongoDatabase = getConnection()
    Dim col As IMongoCollection(Of consulity_Office365MailNotification_UserData)
    col = db.GetCollection(Of consulity_Office365MailNotification_UserData)("Office365MailNotification")

    Dim fltr= Builders(Ofconsulity_Office365MailNotification_UserData).Filter.Eq(Of String)("UId", ud.UId)
    'Dim itm As Newconsulity_Office365MailNotification_UserData
    'col.UpdateMany(fltr, New BsonDocument("$set", NewBsonDocument("value", strNewValue)))

    'Dim upd = Builders(Of consulity_Office365MailNotification_UserData).Update
    'Dim updd = upd.Set(Of String)("LastEmailId",ud.LastEmailId).Set(Of Date)("LastUpdated", DateAndTime.Now)

    'col.UpdateOne(fltr, updd)
    ' updatetimestamp
    ud.LastUpdated =DateAndTime.Now
    col.ReplaceOne(fltr,ud)

    Return "updated record for: " & ud.UId

End Function

Conclusion

The installation of MongoDB and the .NET library ran without problems. The Robomongo UI is a great tool.

One step is unclear: Is MongoDB already available as service w/o running the install command via command line?



Dieter Neumann