5

I want to simply execute pure MongoDB queries via MongoDb 10Gen's .net(c#) driver.

For example . I want to use below command on driver

db.people.update( { name:"Joe" }, { $inc: { n : 1 } } );

I am not sure how can i do this. I am not interested in how to do via high level api classes.

0

2 Answers 2

3
+50

The C# driver (or any other driver) is not intended to "directly" run mongo shell commands. That's what the shell is for. What you need to do is translate the mongo shell commands into the equivalent C# statements.

If you want to run mongo shell commands then run them in the mongo shell.

Sign up to request clarification or add additional context in comments.

2 Comments

C# driver already does not construct shell queries and then send it to mongodb for execute ? If so why driver does not let me to execute shell queries ?
The communication between a driver and the server is via the wire protocol. See: mongodb.org/display/DOCS/Mongo+Wire+Protocol. Even the mongo shell has to translate the mongo shell commands to the wire protocol before sending them to the server.
1

You can construct queries i c# using the fluent Query interface. Those query can then be fired towards the databse using the Find method on a Mongo collection. E.g:

var myDatabase = MongoDatabase.Create(connectionString);
var myCollection = database.GetCollection<MyType>("myCollectionNameInDB");
var myCollection = 
var myQuery = Query.EQ("name", "joe");
var someDataFromDB =  myCollection.Find(myQuery).FirstOrDefault();

Query can also be used with updates. E.g.:

myCollection.Update(
                   myQuery,
                   Update.Replace(new MyType(){...}),
                   UpdateFlags.Upsert
              );

This just replaced the whole document. For finegrained control you can use the Update API combined with the FindAndModify method. E.g:

var myUpdate = Update.Inc("n", 1);
var result = myCollection.FindAndModify(
                   myQuery,
                   SortBy.Descending("name");
                   myUpdate,
                   true // return new document
             );

Check out http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial for more information.

3 Comments

Thanks but i have been asked how to execute pure query via driver
Right, have you tried the Eval method on the database object.
I have been tried few different things on Eval and RunCommand methods but no luck still

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.