Connect and Execute Querries from Visual Basic - VB.NET using C# Driver

2,642 views
Skip to first unread message

Amit De

unread,
Apr 5, 2014, 1:34:12 PM4/5/14
to mongod...@googlegroups.com
So, 

Right now i have the following setup :
1. MongoDB Runnng
2. Drivers for MongoDB
3. Visual Studio 2013
4. MongoVUE

I am able to run querries via CMD and see the output for example
db.collectionname.find({"first_name","james"})

my aim is to do the same using Visual Basic / Visual Studio by entering the above query in a textbox.

I have used the following code :
Imports MongoDB.Bson
Imports MongoDB.Driver
Imports MongoDB.Driver.Builders
Public Class frmMongoDB
    Sub MongoDB()
        Dim client As MongoClient
        Dim server As MongoServer
        Dim db As MongoDatabase
        Dim col1 As MongoCollection
        client = New MongoClient("mongodb://localhost")
        server = client.GetServer()
        db = server.GetDatabase("db1")
        col1 = db.GetCollection("table1")
        Dim query = New QueryDocument("textbox1.text")
    End Sub

So Basically. whatever we do via CMD, i want to do via Visual Studio. Specially the query execution part. 

Also, see'ing the output would be a plus point.

Could find several threads around MSDN and other blogs/forums discussing this, but most of the stuff co-relate to C# or Simply, i am not a great code converter. 

Any help would be really appreciated. Thanks :)

So i would want the exact execution statement.. I could execute using python but i need to figure out the same using VB also. 

A

unread,
Apr 7, 2014, 10:04:21 AM4/7/14
to mongod...@googlegroups.com
Hey,

First, there are a few c# to vb.net code converters (and vice versa): https://www.google.com/search?q=c%23+to+vb.net+converter

For the code, you still need to run something like
col1.Find(Of IEnumerable)(Query.EQ("first_name", "james))
'or
col1.Find(Of IEnumerable)(query) 'based on your example variable

To iterate through it
for each var_user in col1.Find(Of IEnumerable)(query)
'code here
next

Of course the iEnumerable can be any Document Type

Amit

unread,
Apr 10, 2014, 2:09:09 AM4/10/14
to mongod...@googlegroups.com
Hi, THanks for your help.
WHen i add your first line it says

Find is not a member of mongodb driver.

Could you please write a sample sub to execute  a sample query along with the driers or something? I've been stuck with this thing since long and any help will be highly appreciated


Amit

unread,
Apr 10, 2014, 2:11:18 AM4/10/14
to mongod...@googlegroups.com
I just need to execute a query successfully. Don't need the results of it or retrieve anything from it.

A

unread,
Apr 10, 2014, 9:51:54 AM4/10/14
to mongod...@googlegroups.com
Sorry about that

Check the methods of col1 after you hit the .

THere should be
FindOneAs
FindAs
and a few other Find*****

FindAs will get you all the results from your query

Amit

unread,
Apr 10, 2014, 10:07:32 AM4/10/14
to mongod...@googlegroups.com

Ok..thanks. How can I check if its working. Can I use delete or drop a collection the same way ?

A

unread,
Apr 10, 2014, 10:30:56 AM4/10/14
to mongod...@googlegroups.com
Yes, 
.drop gets rid of whole collection
.remove gets rid of the document(s)

Amit

unread,
Apr 10, 2014, 10:35:32 AM4/10/14
to mongod...@googlegroups.com
Thanks a lot again. Can u guide me with the exact statement for drop or remove? 

Actually i want to compare the time of execution of mysql and mongo. Is there a way i can execute using textboxes?

Like col1 would be one text box and the find would be another? 

what's the case for remove or other qurrries?

any help is highly appreciated!!!

Amit

unread,
Apr 10, 2014, 10:48:15 AM4/10/14
to mongod...@googlegroups.com
Also what is this OF IE numerable?

it's not working for remove. I need to test that it works either with insert or remove

A

unread,
Apr 10, 2014, 11:33:10 AM4/10/14
to mongod...@googlegroups.com
iEnumberable is a type, normally you would use a class

I am still fairly new to this, so if anyone please feel free to correct me, but to me it seems the best practice to is to have a class created

Class Car
private property make
private property model
private property color
End Class

col1.findas(of car)(query)   '<-- returns in a format to enumerate through

To insert
dim _newcar as car
_newcar.make="honda"
_newcar.model="accord"
_newcar.model="black
col1,Insert(_newcar)
                   

To remove
col1.Remove(query)

Amit

unread,
Apr 10, 2014, 4:05:26 PM4/10/14
to mongod...@googlegroups.com


Thanks I was able to solve a lot of issues. However I'm still facing issues with querydocument.. The statement format is totally different .. I am not able to run queries like select a from table where b =1

I know the corresponding nosql but how do I write it in query document. It seems to be accepting only format like b,1 where it finds all elements of the document with table1
Is there a way to take ANY query from text box and run it ? If not just help me with the above issue.

Thanks in advance again

A

unread,
Apr 10, 2014, 4:21:43 PM4/10/14
to mongod...@googlegroups.com
col1.findas(of car)(Query.EQ("b","1")).setfields("a":1")

the "1" in a means show, 0 is  not show

Amit

unread,
Apr 12, 2014, 3:40:20 AM4/12/14
to mongod...@googlegroups.com
Hi,
Thanks for helping with the setfields.

This is something i made.

Sub mongoconnect()
        Dim dbname As String
        Dim collname As String
        dbname = txtdbname.Text
        collname = txtcoll.Text

        Dim watch As Stopwatch = New Stopwatch

        Timer1.Interval = 10 '1000ms = 1s
        Timer1.Enabled = True

        Dim mongo As MongoServer = MongoServer.Create()
        mongo.Connect()

        'Print("connected")
        lblstatus.Text = "Connected! Now Executing!!"
        lblstatus.BackColor = Color.SpringGreen
        Dim db = mongo.GetDatabase(dbname)

        Using mongo.RequestStart(db)
            Dim collection = db.GetCollection(Of BsonDocument)(collname)
            'Dim q1 As String = "{}"
            'Dim q2 As String = ""
            Dim query = New QueryDocument("country", "Thailand")
            watch.Start()
            For Each item As BsonDocument In collection.Find(query)
                ' MsgBox("done")
                count = count + 1
                Label1.Text = count
            Next
            watch.Stop()
            lblstatus.Text = "Query Executed Successfully"
            lblstatus.BackColor = Color.SpringGreen


            exetime = watch.ElapsedMilliseconds.ToString()
            Convert.ToInt32(exetime)
            AddHandler Timer1.Tick, AddressOf Timer1_Tick 'add handler

        End Using
End Sub

Please suggest me on how to add set fields here?

Also, are the queries being executed event if i dont write anything in the for loop? Is the execution time i am calculating right?
If i want to add mo ore to it, like sort of anything as such.. How do i improve the query? Sorry if i am asking too many questions, the way of representation is much more different in this driver than the usual way of wriitng mongoDB qurries.

Is there no way of triggering the qurries straight as in case of MYSQL i just made a text box and i am entering all the qurries with all formatted strings there itself.  Is there a way i can enter the entire MongoQueyy in a text way just the way mongoDB reads it and then execute it?


Thanks again

A

unread,
Apr 13, 2014, 11:07:38 AM4/13/14
to mongod...@googlegroups.com
 For Each item As BsonDocument In collection.Find(query).setFields

But is there a .Find method ?  I thought there was FindAs, FindOneAs and similar to that
Reply all
Reply to author
Forward
0 new messages