I am trying to extract the last five documents from the Deal collection in MongoDB using the C # driver for MongoDB. I can do this using the code below.
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; }
But I want to get only the last 5 documents, and FindAll () loads all the documents in the collection. I tried to do this with Find (), but for this I need a query as a parameter. How can I write a query for "orderby" in the Mongo driver to sort C #?
stack overflow.squite But the accepted answer does not work for me.
c # mongodb mongodb-.net-driver
har
source share