views:

165

answers:

2

I'm trying to figure out how to sort a collection of documents server side by telling the C# driver what the sort order is, but it appears not to support that construct yet.

Is it possible to do this any other way?

+1  A: 

I'm doing this in JavaScript since I don't know C#, but it should have equivalent syntax with the C# driver.

If your query looked like:

db.c.find({"foo" : "bar"})

and you want to sort by "baz" ascending, wrap your query in a "query" field and add an "orderby" field:

db.c.find({"query" : {"foo" : "bar"}, "orderby" : {"baz" : 1}})

For descending sort, use -1.

kristina
+2  A: 

It seems the way to do this using the existing C# driver is as follows:

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

Which I was turned on to by Sam Corder here

jmcd