C#MongDB数据库取某时间段内的数据

时间:2021-09-14 02:18:53
 BsonDocument bsonDoc = new BsonDocument();
            bsonDoc.Add("TimeData", new BsonDocument() { { "$gte", DateTimeToInt(StartTime) }, { "$lte", DateTimeToInt(EndTime) } });//StartTime,EndTime分别为起始时间和终止时间
            var collection = MoDataBase.GetCollection<BsonDocument>(string.Format("{0}_{1}_Day", StatioID, PullutionID));
            var sort = Builders<BsonDocument>.Sort.Ascending("TimeData");
            var list = collection.Find(bsonDoc).Sort(sort).ToList();
 /// <summary>
        /// 将取到的时间转为Int
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        private static BsonInt64 DateTimeToInt(string datetime)
        {
            return BsonInt64.Create(System.DateTime.Parse(datetime).ToString("yyyyMMddHHmmss"));
        }