После нескольких часов работы вот мой оптимизированный ответ.
Изменения в методе GetData с использованием метода Aggregate:
match.Add();
project.AddRange(new BsonDocument {
{ "TDLNumber", 1 },
{ "createdon", 1 },
{ "modifiedon", new BsonDocument("$dateToString",
new BsonDocument("format", "%Y-%m-%d").Add("date", "$modifiedon"))// format like 2019-06-27
}
});
BsonDocument expAddfield = new BsonDocument(new BsonDocument("$addFields", new
BsonDocument("createdon", new BsonDocument("$dateToString",
new BsonDocument
{
{ "date", "$createdon" },
{ "format", "%Y-%m-%d" }
}))));
sort.Add("createdon", -1);
List<BsonDocument> lstReslut= dbo.FindAggDynNoGroupWithSortSkipLimit(watertrackingCollection, expProject, match1, expAddfield, sort, skip, limit);
QueryResult = lstReslut.ToJson();
Aggregate Method
public List<BsonDocument> FindAggDynQuery(string collectionName, BsonDocument expProject, BsonDocument expMatch, BsonDocument expAddfield, BsonDocument expSort, int skip, int limit)
{
var connectionManager = new ez2Track.Data.ConnectionManager();
var _collection = connectionManager.GetDBCollection(collectionName);
var agg = _collection.Aggregate().Project(expProject).Match(expMatch).AppendStage<BsonDocument>(expAddfield).Sort(expSort).Skip(skip).Limit(limit);
var result = agg.ToListAsync().Result;
return result;
}