Friday 19 May 2017

How to 'split up' API Calls in MEAN app / Handling large documents

I am new to the MEAN stack and I am facing the issue that my data from mongoDB is very big, and exceeds the maximum document size of 16MB everytime I want to do some aggregation before loading the site.

I have my routes.js structured like this:

app.get('/stations', function(req,res) {
    var aggr = Station.aggregate([
        {
            "$project": {
                "StationName"  : 1,
                "Latitude"     : "$Latitude",
                "Longitude"    : "$Longitude",
                "Primary"      : "$Primary Type",
                "Secondary"    : "$Secondary Type",
                "Tertiary"     : "$Tertiary Type",
                "Trips_out"    : "$trips" // Very big
            }
        }
    ])
    aggr.options = { allowDiskUse: true };
    aggr.exec(function(err, stations){
      if(err)
        res.send(err);
      res.json(stations); 
    });
  });

The Problem now is that this aggregation fails, it is too big. I still need the data though, and actually more, I already cut down a lot. This data is needed to display stuff client side in the browser.

I have trouble finding information on how this is typically handled when building a MEAN app. Do I need to split up the API calls, e.g. define another API for the "trips" and call that from my angular factory once it is needed? What is best practice for this?



via ffritz

No comments:

Post a Comment