mbdavid / LiteDB

LiteDB - A .NET NoSQL Document Store in a single data file
http://www.litedb.org
MIT License
8.62k stars 1.25k forks source link

[QUESTION] How to use paging effectively in V5 #1475

Closed GitOffice closed 4 years ago

GitOffice commented 4 years ago

LiteDB is convenient and effective and I can't help but continue using it in the mobile app. In V4, I have created index and use engine.FindAll to get all results and then page the collection with Linq-to-Object Skip.

Users have to get the next page with Pull-to-Refresh because the pagination of search results doesn't show on the mobile screen.

It is the drawback that app will hit the db with engine.FindAll repeatedly even if the query parameters are the same and the page number is only different.

How can I use the paging effectively except in-memory in V5?

Another question: Is there any tutorial for use of LinqKit's PredicateBuilder in V5?

Thanks for respectable team and great work

mbdavid commented 4 years ago

Hi @GitOffice, in v5 you can use Query() fluent API to do that. v5 has a power query engine that implement some in-database features (not present in v4) like Multiples Where/OrderBy/Skip/Limit/GroupBy/Select... it's much more flexiable than v4

lbnascimento commented 4 years ago

@GitOffice, one way to do it would be using the Query method in LiteCollection.

var col = db.GetCollection("customers");

var query = col.Query();
var resultsPage = query.Limit(entriesPerPage).Offset((pageNumber - 1) * entriesPerPage).ToEnumerable();

The query will only be run when ToEnumerable is called, so you can call it multiple times with different limit and offset values. If there is an index over the filter expression, the index will be used and only the desired documents will be accessed (in my example, no filter expression is present, which means the query will use the index over the _id field).

Is this answers your question, please close the issue.

sgf commented 1 year ago

@GitOffice, one way to do it would be using the Query method in LiteCollection.

var col = db.GetCollection("customers");

var query = col.Query();
var resultsPage = query.Limit(entriesPerPage).Offset((pageNumber - 1) * entriesPerPage).ToEnumerable();

The query will only be run when ToEnumerable is called, so you can call it multiple times with different limit and offset values. If there is an index over the filter expression, the index will be used and only the desired documents will be accessed (in my example, no filter expression is present, which means the query will use the index over the _id field).

Is this answers your question, please close the issue.

but how get the total page count or total-items count?