Open mamadi opened 8 years ago
db.ratingcollection.aggregate([ {$match: {"Spot":"Starbucks-Irvine-5"}}, {$group: { _id: "Spot", avgWifi: {$avg: "$Ratings.Wifi"}, avgStaff: {$avg: "$Ratings.Staff"}, avgCoffee: {$avg: "$Ratings.Coffee"}, avgSeating: {$avg: "$Ratings.Seating"}, avgParking: {$avg: "$Ratings.Parking"}, avgOveral: {$avg: "$Ratings.Overall"} }}])
Do we need this? or just do it at rating insert/update time?
/* GET spot average ratings from ratings collection and update them in the spots collection */ router.get('/ratings/:spot', function(req, res) { var db = req.db; var spotIndex = req.params.spot;
collection = db.get('spotcollection');
collection.findOne({index: spotIndex},{},function(err,currRatings){
if (err) {
console.log('Could not find spot [' + spotIndex + '] in DB');
res.json({ result: 'Could not find spot [' + spotIndex + '] in DB' });
} else {
console.log('Found spot ' + spotIndex + ' in DB');
updateSpotAvgRatings(db, currRatings, spotIndex, res);
}
});
});
There needs to be a periodic job that take the average and updates the spot ratings accordingly.