IMongoCollection_collect; string mongoDB_Host = ConfigurationManager.AppSettings["MongoDB"].ToString(); string mongoDB_DbName = ConfigurationManager.AppSettings["MongoDB_DBNAME"].ToString(); MongoDB.Driver.MongoClient mc = new MongoClient(mongoDB_Host); IMongoDatabase _db = mc.GetDatabase(mongoDB_DbName); _collect = _db.GetCollection (TableName); BsonDocument dbMatch = new BsonDocument { { "Day", new BsonDocument("$eq", Day) }, { "StartTime", new BsonDocument("$lt", StartTime) } }; BsonDocument dbGroup = new BsonDocument { { "_id", "$EmployeeId" }, { "maxStartTime", new BsonDocument("$max", "$StartTime") } }; var aggregate = _collect.Aggregate().Match(dbMatch).Group(dbGroup); List list222 = aggregate.ToList (); if (list222 != null && list222.Count > 0) { // NLogger.Error("分组总和:-- " + list222.Count); foreach (var itt in list222) { gUserLocation g = new gUserLocation(); g.EmployeeId = Convert.ToInt32(itt["_id"].ToString()); g.StartTime = Convert.ToInt64(itt["maxStartTime"].ToString()); gUserLocationlist.Add(g); } }
参考:
http://www.cnblogs.com/shaosks/p/5816932.html
http://www.cnblogs.com/lizeyan/p/5411708.html
http://blog.csdn.net/shiyaru1314/article/details/52442957