MongoDb NORM - Pagination and Total Documents
Using the MongoDb NORM driver, does anyone know whether it's possible to get the cursor to a 'queried' collection similar to below, so that a 'page' of documents can be retrieved as well as the total number of queried documents?
> var j = db.People.find().skip(2).limit(2)
> j.count()
13
> j
{ "_id" : NumberLong(25), "Name" : "Ted" }
{ "_id"开发者_开发技巧 : NumberLong(26), "Name" : "Tom" }
As I assume that the following performs the MongoDbquery twice...
totalItems = peopleCollection.Count(queryExpando);
peopleList = peopleCollection.Find(queryExpando, orderByExpando, pageSize, startIndex).ToList();
I am not sure if there is another way, but you can probably use LINQ to do what you need:
var allItems = peopleCollection.AsQueryable();
var count = allItems.Count();
var peopleList = allItems
.Where(p => p.Field == fieldValue)
.OrderBy(p => p.OrderByField)
.Skip(startIndex)
.Take(pageSize);
精彩评论