Re: MongoDb driver question

335 views
Skip to first unread message

Sam Corder

unread,
Jan 7, 2010, 5:50:15 PM1/7/10
to Okku Touronen, mongodb...@googlegroups.com
No problem.  The driver doesn't have many convenience methods yet such as for sorting.  You have to format the proper query yourself and send it to one of the find methods.

db.collection.find().sort({name:1, age:-1}); in the shell issues this query to the database:
{ query: {}, orderby: { name:1.0, age:-1.0} }
Every {} pair is another document so the equivalent c# code would be:

db["collection"].Find(new Document().Append("query", new Document()).Append("orderby", new Document().Append(name:1).Append(age,-1)));

It is a little long but for now that is the way you have to do it.  After some more of the major features on the todo list are out of the way I'll be circling around and adding some more of the convenience methods.

-Sam

P.S. I hope you don't mind.  I cc'ed the list so that others can find the answer later.

On Thu, Jan 7, 2010 at 5:32 PM, Okku Touronen <> wrote:
Hello Sam, I have been using your mongodb driver and like it a lot.

Sorry for this silly question: How do I use it for sorting?

Like this:
db.collection.find().sort({name : 1, age : -1})

Best regards Okku Touronen

GWBasic

unread,
Feb 7, 2010, 2:03:12 AM2/7/10
to mongodb-csharp
Is this what I need to do for hint? (See
http://www.mongodb.org/display/DOCS/Optimization#Optimization-Hint)

spec["query"] = the query
spec["hint"] = the hint
foreach (Document d in Collection.Find(spec).Documents)
...

Sam Corder

unread,
Feb 7, 2010, 1:37:01 PM2/7/10
to mongodb...@googlegroups.com
That looks right.  Often what I do is run my server with the verbose switch from a command line and then try out the operation in the shell.  The query will get printed to the console and you'll be able to construct a query that does the same thing.

-Sam

GWBasic

unread,
Feb 8, 2010, 10:41:11 PM2/8/10
to mongodb-csharp
It's
spec["$query"] = the hint
Reply all
Reply to author
Forward
0 new messages