我想从MongoDB的“Deal”集合中使用MongoDB的C#驱动程序检索五个最近的文档。我可以用下面的代码来完成。如何在Mongo驱动程序中为C#排序查询“orderby”?
public IList<TEntity> GetRecentFive()
{
IList<TEntity> entities = new List<TEntity>();
using (MongoDbContext dbContext = new MongoDbContext(_dbFactory))
{
var cursor = dbContext.Set<TEntity>().FindAll().SetSortOrder(SortBy.Descending("ModifiedDateTime")).SetLimit(5);
foreach (TEntity entity in cursor)
{
entities.Add(entity);
}
}
return entities;
}
但我只想得到最近的5个文档,FindAll()加载集合中的所有文档。我试图用Find()来做,但它需要查询作为参数。如何在Mongo驱动程序中为“#orderby”编写查询以便C#排序?
https://stackoverflow.com/a/2148479/778101在这里问了一个类似的问题。但接受的答案不适合我。