I’ve found c# code like this (Collection is of type MongoCollection<T> here):
Collection.AsQueryable()
.OrderByDescending(i => i.SomeField)
.Where(i => i.OtherField == "bla-bla")
does not run both sorting (OrderBy) and filtering (Where) on a server as I expected, instead it calls sorting with MongoDb engine and all further filtering is processed on a client side, which takes fair amount of time. That what I’ve found in Mongodb profiler as a result of the code execution:
"query" : {
"$query" : { },
"$orderby" : {
"SomeField" : -1
}
}
Note $query is empty. Well, is there any idea over how to make it work on database (both sorting and filtering)?
To order the items, all have to be retrieved, so if you specify that as the first operation I’d expect the behaviour you observed. So you’d have to reverse your statement (as you do in the
IMongoQuerysyntax). This can also be done in LINQ syntax, but sinceWherereturnsIEnumerable, you need an extra cast: