Efficient Pagination with Flask and Mongoengine - pagination

I have a Flask app with which I would like to display paginated tables of data from a MongoDB Collection. However, there are potentially very many documents in this collection, so I would like to be loading them lazily - only loading the ones which are about to be displayed.
My problem is that on one page in my app, I would like to paginate:
Stuff.objects()
But on different pages I would like to paginate:
Stuff.objects(__raw__=query) or Stuff.objects(message__in=Message.objects(__raw__=query))
Calling any of those particular functions automatically loads all of the relevant objects into memory (as I discovered by running locals()) so I need to paginate the calls with:
Stuff.objects().skip(number).limit(pagelength), or
Stuff.objects(__raw__=query).skip(number).limit(pagelength)
So it would seem that I need a Paginator class which I can simply pass Report into, then somehow specify the query information.
Can anyone recommend a solution?

Try using the paginator from flask-mongoengine You can use it like so:
paginator = Pagination(Post.objects, 1, 10)
print paginator.items

Related

nunjucks not outputting all data from MongoDB

I am using node.js as a scripting language and I am also using nunjucks as the template engine. I have a weird situation going where some of the data that I retrieve from the MongoDB database is not being printed into document.
As you see hear these are the values that I want to print to the web page
But when you look at the web page, only some of the data has been printed out and the other information is missing.
I console.logged the data to prove that the values are in the database
A weird thing is that if you write the whole object into the code, like so
It will output all the data in one block. That includes the month, the year, and slug property that I am trying to output to the page. Yet, it only does that if I print out the whole object
I found out what was the problem. In my mongoose schema, I did not have the properties listed like slug, month, or date. So when I tried to retrieve data from those properties it did not output them. So If you are having similar problems make sure the properties are declared in your mongoose schema

MongoDB NodeJS Query and paginate multiple collection

I am dealing with a case where I need to render of listing of several entities on one page, with one pagination.
I.g.: Given 3 collections, People, Robots and ETs. Now I'd need to list them all in a /everybody route that renders a listing page with a pagination and some filters.
As far as I can tell the only way to do that in MongoDB is to query each collection manually and handle the pagination in the application. Are there any other/better ways ?

Best async practices for Jade template pages created from objects in mongodb

I have this website run on node.js using jade for page templates and mongodb to store some data. One of the pages is this 'parts' page. It is just a list of parts I use in some project, but that's not important.
So i have this scenario where:
I add an object in the form of a doc to the DB
That updates a global array of those objects
That global array gets passed to the jade template which draws the list for each element in the global array of objects.
On server startup, should the server ever restart, it populates the global by querying the DB once.
Here's my question,
Is this the best practice I can use in an async environment like node?
My rational is that i'm reducing the amount of time spent communicating with the DB by keeping the docs in the app.
Would it be better to query the DB for the docs and pass that to the template each time a user loads a page?
is using a global like this even async or just too fast to tell?
note: it's only for few records; say less then 200 records.
note: there will never be more than 1 person adding to the array at a time.
edit: third option, is this method bad all together?

Zend Framework 2 Paginating Search Results

I'm making a complex search form with a lot of inputs to query and I want to paginate the search results using Zend Framework 2.
I've found a few people who are trying to do something similar but I have yet to find a good solution.
Some people suggested switching to $_GET method instead of $_POST but i would prefer to avoid this because of the number of form elements.
The results are coming up fine, but as soon as i try to navigate to the second page, the query is lost and it is essentially paginating all records on the table.
What is the best way to store the original search query so that the paginated results are the actual results?
I can't imagine ZF2 doesn't have an easy way to paginate $_POST results but I haven't been able to figure it out yet
well you either need to repost the search parameters on every page request or keep the search parameters in a session and on the second request check if the request is get or post and use the session if it is get

Jade renders unexpected values in templates for "populated" mongoose properties

I have a jade template where I'm printing out properties from a mongoose object. The object has properties that were populated via mongoose's dbref-like populate feature. When The template renders I get nonsense.
simplified example:
in the route
User.findById(req.params.user, function(err, user){
res.render('training', {training:user.training});
}).populate('training.details');
in the template:
a.training-link(href='/training/#{training.details.id}') ...
in the browser:
...
The first time the page is loaded, where I would expect a regular mongo hexadecimal string (4fac4e5f379cb0a68100015d) I get something like "O >°Cm5�". This only happens on the first page load after a server restart. All subsequent page loads render as expected.
What's happening here and how can I fix it?
Thanks.
This is a bit embarrassing but I figured it out.
There is a logical branch in our app that, for various reasons, causes the code in my question above to run on the second and subsequent loads but not on the first. The other branch, which only runs on the first load, makes a similar query but I hadn't added populate('training.details') to it. I believe the nonsense string O¬N_7°¦� is the result of calling .id on an ObjectId object rather than the populated training.details.
Looks like we need to refactor some code.

Resources