Prasad Kasturi
Prasad Kasturi

Reputation: 31

MongoDb, NodeJs, Express and Angular 2, Data from two collections join, retreival and display

I have two mongo collections:

I would like to retrieve the data

carAvailability: {locationCity, locationState, locationZip, carMake, carType}

In my nodejs layer, Here is what I am trying to do:

First, I am trying to retrieve the array of locationIds by locationCity, locationState or locationZip. Once I have array of locationIds, I am trying to retrieve the carLocations.

app.get("/api/carsbylocationids", function (req, res) {
  //console.log(" locationid:" + req.query.locationid);
  var locationIds  = JSON.parse(req.query.locationIds);
  console.log("locationIds: " + locationIds);
  console.log("carId: " + req.query.carId);
  db.collection(carsLocations).find(
    {
      'locationId': {$in:locationIds},
      'carId': req.query.carId
    }
  ).toArray(function (err, docs) {
    if (err) {
      handleError(res, err.message, "Failed to get items.");
    } else {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'GET);
      res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
      res.status(200).json(docs);
    }
  });

});

Either in this part or the angularJS 2 code (not provided here), I would like the carAvailability: {locationCity, locationState, locationZip, carMake, carType} so that I can iterate through and display on the screen.

I am unable to arrive on a best/efficient way to do it. Should I handle this join type of condition in nodeJS layer or Angular2 layer?

Upvotes: 2

Views: 694

Answers (1)

Prasad Kasturi
Prasad Kasturi

Reputation: 31

I used the aggregation by $lookup and $match to get the data from mongoDb. For those of you who want specifics:

app.get("/api/locationsByitemid", function (req, res) {
  console.log(" itemid:" + req.query.itemid + " city:" + req.query.locationCity + " state:" + req.query.locationState);

  db.collection(LOCATIONS_COLLECTION).aggregate([
    { $lookup: { from: ITEMS_COLLECTION, localField: "locationId",foreignField: "locationId", as: "locationsbyLocationId"} },
    { $unwind:"$locationsbyLocationId"},
    { "$match": { 'locationsbyLocationId.itemid': req.query.itemid, 'locationCity': req.query.locationCity, 'locationState' : req.query.locationState} }
    ]
  ).toArray(function (err, docs) {
    if (err) {
      handleError(res, err.message, "Failed to get items.");
    } else {
      res.header('Access-Control-Allow-Origin', '*');
      res.header('Access-Control-Allow-Methods', 'GET');
      res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
      res.status(200).json(docs);
    }
  });

});

This is working to my satisfaction but any suggestions are welcome.... Thanks to those who pointed out that this is best handled prior to RestFul Service call. Thats what I explored.

Upvotes: 1

Related Questions