Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Trying out MongoDB persisting objectsPhone Number Primitive »
    comments

    I guess this NoSQL thing is getting to me. So I had to try it and see if I can use it with my favorite language.

    I first did a little research on the net and found this post.

    I went on from there.

    First I downloaded MongoDB.

    Then you unzip it and go to the bin folder and run mongod.exe. By carefull with firewall issues.

    Now we create a folder named c:\data\db

    then I downloaded a driver for .net.

    I then referenced the three dll's. Just to be sure ;-).

    MongoDB.Driver.dll
    MongoDB.GridFS.dll
    MongoDB.linq.dll

    So far no problems. Now lets create a console application and see how easy it is to set up.

    1. Imports MongoDB.Driver
    2.  
    3. Module Module1
    4.  
    5.     Sub Main()
    6.         Dim mongo As New Mongo
    7.         mongo.Connect()
    8.         Dim db = mongo.GetDatabase("person")
    9.         Dim persons = db.GetCollection("persons")
    10.         Console.WriteLine("You have " & persons.Count & " persons in your database")
    11.        
    12.         Console.ReadLine()
    13.     End Sub
    14.  
    15. End Module

    If you run this you should get

    You have 0 persons in your database

    Now lets add a person to our database.

    1. Dim person As New Document
    2.         person("name") = "name1"
    3.         person("firstname") = "firstname2"
    4.         persons.Insert(person)
    5.         Console.WriteLine("You have " & persons.Count & " persons in your database")

    The above code should now give you the following result.

    You have 0 persons in your database
    You have 1 persons in your database

    Cool.

    But the second time you run it you get this.

    You have 1 persons in your database
    You have 2 persons in your database

    And the third

    You have 2 persons in your database
    You have 3 persons in your database

    And so on...

    So time to delete some stuff.

    1. persons.Delete(New Document)
    2.         Console.WriteLine("You have " & persons.Count & " persons in your database")

    see the delete with the New Document parameter? That deletes all documents from the database.

    So the result will now be.

    You have 3 persons in your database
    You have 4 persons in your database
    You have 0 persons in your database

    Easy.

    Now lets look for a certain record.

    1. Dim mongo As New Mongo
    2.         mongo.Connect()
    3.         Dim db = mongo.GetDatabase("person")
    4.         Dim persons = db.GetCollection("persons")
    5.         Console.WriteLine("You have " & persons.Count & " persons in your database")
    6.         Dim person As New Document
    7.         person("name") = "name1"
    8.         person("firstname") = "firstname1"
    9.         persons.Insert(person)
    10.         Dim person2 As New Document
    11.         person2("name") = "name2"
    12.         person2("firstname") = "firstname2"
    13.         persons.Insert(person2)
    14.         Console.WriteLine("You have " & persons.Count & " persons in your database")
    15.         Dim persontofind = New Document
    16.         persontofind("name") = "name1"
    17.         Dim personfound = persons.FindOne(persontofind)
    18.         Console.WriteLine("Found person: " & personfound("name") & " " & personfound("firstname"))
    19.         persons.Delete(New Document)
    20.         Console.WriteLine("You have " & persons.Count & " persons in your database")
    21.         Console.ReadLine()

    Now the result of the above will be.

    You have 0 persons in your database
    You have 1 persons in your database
    Found person: name1 firstname1
    You have 0 persons in your database

    All very simple untill now. Only took me a couple of minutes to figure out.

    More on this later.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    mongodb, vb.net
    InstapaperVote on HN

    6 comments

    Comment from: SQLDenis [Member] Email
    SQLDenis How would you import a file with let's say 100000 rows, is there a facility like bulk insert or do you have to read the file in and then insert one by one.

    Or maybe can you read the file into a collection/array and can you then insert with one statement.

    Also are transactions supported.

    How do you do joins? How do you do aggregate calculations, for example total amount of all orders in March 2010...is that possible?

    03/27/10 @ 07:45
    Comment from: Christiaan Baes (chrissie1) [Member]
    Christiaan Baes (chrissie1) I don't think you must compare this with RDBMS. I think it has a different set of pros and cons and it's own area of use.

    I think it would be ideal for a document database where you let the user ad fields on a document per document basis.
    03/27/10 @ 08:04
    Comment from: SQLDenis [Member] Email
    SQLDenis So is this like a key value DB?
    03/27/10 @ 08:29
    Comment from: Christiaan Baes (chrissie1) [Member]
    Christiaan Baes (chrissie1) Yes.
    03/27/10 @ 08:46
    Comment from: RC [Visitor]
    RC 1. Here another driver for .net, (still beta). This driver makes it possible to serialize your object into the database. http://github.com/atheken/NoRM

    2. MongoDB has a much richer query language than a key value store. And it also has the possibilty to index the nested objects and the elements of nested arrays of a document.
    04/23/10 @ 22:16
    Comment from: GC [Visitor]
    GC I have a question, if I have an array, I want to add that array ot the database, then reference items out of it that contain the search criteria, not just the things that are equal to it. Right now I have...



    Dim mongo As New Mongo

    mongo.Connect()

    Dim db = mongo.GetDatabase("link")
    Dim links = db.GetCollection("link")
    Dim link As New Document

    For counter As Integer = 0 To arrayResults.Length
    If arrayResults(counter) = Nothing Then
    Exit For
    Else
    link("link") = arrayResults(counter)
    links.Insert(link)
    End If
    Next

    Dim linktofind = New Document
    linktofind("name") = "http://"
    Dim linkfound = links.Find(linktofind)
    lsTest.Items.Add(linkfound.ToString)
    10/29/11 @ 08:44

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)