Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement server side rendering datatable, Using node and mongo db?

So i have one user collection(mongo DB) which consists millions of user.

I m using nodejs as backend, angular js as frontend and datatable for displaying those users.

But datatable Load all users in one api call which load more then 1 million user.

This makes my API response two slow.

I want only first 50 users then next 50 then so on....

Server stack = node js + angular js + mongo DB

Thanks

like image 459
Mahesh Gareja Avatar asked Jan 20 '17 13:01

Mahesh Gareja


2 Answers

If you are using datatable with huge amount of data you should consider using server side processing functionnality.

Server side processing for datatable is described here : https://datatables.net/manual/server-side

But if you feel lazy to implement this on your server you could use third parties like :

  • https://github.com/vinicius0026/datatables-query
  • https://github.com/eherve/mongoose-datatable

Hope this helps.

like image 155
OlivierTo Avatar answered Nov 12 '22 07:11

OlivierTo


The way to solve you client trying to fetch users from your server(and DB) and then rendering them to a datatable is done using pagination. There a few ways of solving pagination which i have seen, let's assume you are using REST.

One way of doing this is having your API ending with:

/api/users?skip=100&limit=50

Meaning, the client will ask your server for users(using default sorting) and skipping the first 100 results it finds and retrieving the next 50 users.

Another way is to have your API like this(I don't really like this approach):

/api/users?page=5&pageSize=50

Meaning, the client will pass which page and how many results per page it wants to fetch. This will result in a server side calculation becuase you would need to fetch users from 250-300.

You can read on pagination a lot more on the web.

Having said that, your next issue is to fetch the desired users from the database. MongoDB has two functions for using skip and limit, which is why I like the first API better. You can do the query as follows:

users.find().skip(50).limit(50)

You can read more about the limit function here and the skip function here

like image 38
guymaor86 Avatar answered Nov 12 '22 08:11

guymaor86