我需要将ISODate转换为类似于“ 2019-06-27”这样的字符串格式,并且还需要按日期对其进行排序。但是,我已经以必需的格式转换了日期,但是由于日期字符串格式被更早地转换,所以弄乱了日期排序。
应用程序环境
这是将文档存储在MongoDB集合中的方式:
{
"_id": "9d78d0e8-7b13-4487-88a3-d91d64f29b38",
"Number": "001",
.......,
.......,
"createdon": {
"$date": "2019-09-19T00:00:00.000Z"
},
"modifiedon": {
"$date": "2019-12-17T19:52:00.000Z"
}
}
这是有效的C#函数,但没有日期排序:
public string GetData(Data.DataModel.TestModel model)
{
string collectionName = "testCollection";
BsonDocument sort = new BsonDocument();
BsonDocument match = new BsonDocument();
BsonDocument project = new BsonDocument();
int skip = model.skip;
int limit = model.limit;
try
{
match.Add();
project.AddRange(new BsonDocument {
{ "TDLNumber", 1 },
{ "createdon", new BsonDocument("$dateToString",
new BsonDocument("format", "%Y-%m-%d").Add("date", "$createdon")) // format like 2019-06-27
},
{ "modifiedon", new BsonDocument("$dateToString",
new BsonDocument("format", "%Y-%m-%d").Add("date", "$modifiedon"))// format like 2019-06-27
}
});
sort.AddRange(new BsonDocument { { "createdon", -1 }, { "modifiedon", -1 } });
List<BsonDocument> lstReslut = dbo.FindAggDynQuery(collectionName, match, project, skip, limit, sort);
QueryResult = lstReslut.ToJson();
}
catch (Exception)
{
QueryResult = "[]";
}
return QueryResult;
}
但是如果我不进行日期转换,则可以像这样完美地工作
{ "createdon", 1},
{ "modifiedon",1},
// { "createdon", new BsonDocument("$dateToString",
// new BsonDocument("format", "%Y-%m-%d").Add("date", "$createdon")) // format like 2019-06-27
// },
//{ "modifiedon", new BsonDocument("$dateToString",
// new BsonDocument("format", "%Y-%m-%d").Add("date", "$modifiedon"))// format like 2019-06-27
//}
这里是数据库层查询功能:
public List<BsonDocument> FindAggDynQuery(string collectionName, BsonDocument find, BsonDocument project, int skip, int limit, BsonDocument sort)
{
using (var connectionManager = new Test.Data.ConnectionManager())
{
var _collection = connectionManager.GetDBCollection(collectionName);
var result = _collection.Find(find).Sort(sort).Project(project).Skip(skip).Limit(limit).ToListAsync().Result;
return result;
}
}
这里出了什么问题。任何帮助将不胜感激!
经过几个小时的工作,这是我的最佳答案。
[使用Aggregate方法更改GetData方法:
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();
汇总方法
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;
}