I'm using MongoDB for sometime to perform all sort of fast inserts or having as a log, but I'm having some trouble to get a really simple query
How, in Mongo, would I do to get a similiar to this T-SQL
SELECT TOP 1 [date] FROM [Collection] ORDER BY [date] desc
In other words, what is the last date in the collection.
I'm trying to use FindOne
or any other that can return one document, but none accepts a sortBy
property... how would I do this?
var query = Query.EQ("status", "pending");
var sortBy = SortBy.Descending("date");
return collectionLog.FindOneAs<BsonDocument>(query, sortBy);
The last line above would be perfect, but this method only accepts the query
parameter.
There is no .SetSortOrder()
method of FindOneAs
in the C# driver. This is because FindOneAs
returns a document while .SetSortOrder()
is a member of MongoCursor
.
The equivalent query would be something similar to:
var query = Query.EQ("status", "pending");
var sortBy = SortBy.Descending("date");
return collectionLog.FindAs<BsonDocument>(query).SetSortOrder(sortby).SetLimit(1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With