How to fliter a collection using VB.Net

1,090 views
Skip to first unread message

Mark Lambie

unread,
Feb 5, 2016, 5:59:45 PM2/5/16
to mongodb-user
Anyone have samples of filtering collections using VB.net.

Wan Bachtiar

unread,
Feb 7, 2016, 7:23:37 PM2/7/16
to mongodb-user

Hi Mark,

Check out the MongoDB C# Driver Quick Tour, the VB.net examples should be similar to some of the examples there.

Below is a simple example on how to query a collection in VB to get you started:

Imports MongoDB.Bson
Imports MongoDB.Driver

Module QueryExample
    Sub Main()

        Dim client = New MongoClient("mongodb://localhost:27017")
        Dim db = client.GetDatabase("dbName")
        Dim collection = db.GetCollection(Of BsonDocument)("collectionName")

        'Find the first or default from the collection'
        Dim document = collection.Find(New QueryDocument()).FirstOrDefault()
        Console.WriteLine(document.ToString())

        'Find all documents from collection matching field=value'
        Dim documents = collection.Find(New QueryDocument("field", "value"))
        For Each doc As BsonDocument In documents.ToList()
            Console.WriteLine(doc.ToString())
        Next

    End Sub
End Module

The snippet above is written using mongocsharpdriver-2.2.3 and .NET v4.5.2

Best regards,

Wan.

Reply all
Reply to author
Forward
0 new messages