C# 驱动的mongodb的分页查询简单示例
/// <summary> /// mongodb分页查询 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sortQuery"></param> /// <param name="query"></param> public void GetPaging<T>(Dictionary<string, string> sortQuery, Expression<Func<T,bool>> query) { if (sortQuery.Count < 0) { return; } IMongoClient mongoClient = new MongoClient(""); IMongoDatabase mongoDatabase = mongoClient.GetDatabase(""); var sort = Builders<T>.Sort; SortDefinition<T> sortimp = null; foreach (var item in sortQuery) { if (item.Value == "d") { sortimp = sort.Descending(item.Key); } else { sortimp = sort.Ascending(item.Key); } } mongoDatabase.GetCollection<T>(typeof(T).Name).Find<T>(query).Sort(sortimp)?.Limit(1).Skip(1); }
积累小的知识,才能成就大的智慧,希望网上少一些复制多一些原创有用的答案