MongoDB 使用 C# 驱动程序 - 带排序的日期格式

MongoDB with C# Driver - date format with sort

我需要将 ISODate 转换为字符串格式,例如“2019-06-27”,并且还需要按日期对其进行排序。但是,我已将日期转换为所需格式,但由于日期字符串格式较早转换,导致日期排序混乱。

应用环境

  1. .NET MongoDB 驱动程序 - 2.0
  2. MongoDB 版本 - 3.2

这是 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;
        }
    }

这里出了什么问题。任何帮助将不胜感激!!

经过几个小时的工作,这是我的优化答案。

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;
    }