我如何在MongoDB中收集的最后一个项目? [英] How do I get the last item in a MongoDB collection?

查看:151
本文介绍了我如何在MongoDB中收集的最后一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MongoDB的某个时候进行的所有排序快速插入或有作为的日志,但我有一些麻烦让一非常简单的查询

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

如何在蒙戈,我会做的就是一个类似这个T-SQL

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.

我试图用 FindOne 或其他任何可以返回一个文档,但没有接受 sortBy 属性...我会怎么做呢?

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.

推荐答案

有没有 .SetSortOrder()在C#驱动程序 FindOneAs 。这是因为 FindOneAs 返回一个文档,而 .SetSortOrder()是成员 MongoCursor

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.

等效查询是类似于:

var query = Query.EQ("status", "pending");
var sortBy = SortBy.Descending("date");

return collectionLog.FindAs<BsonDocument>(query).SetSortOrder(sortby).SetLimit(1);

这篇关于我如何在MongoDB中收集的最后一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆