Struggling to incorporate one of MongoDB's built-in sorting algorithms into a Model.find()
? It seems easy to understand and apply it in the mongo command line, but implementing it within your code remains a challenge.
While the existing code functions perfectly and retrieves the desired values, you now aim to introduce the stored sort from mySort
to organize the data being fetched.
const express = require('express'),
router = express.Router(),
House = require('../models/house'),
Event = require('../models/event');
router.get('/', function(req, res){
var mySort = {totalPoints: -1};
House.find({}, function(err, houses){
if(err){
console.log(err);
res.redirect('/error');
} else {
res.render('houses/index', {pageTitle: 'Houses', houses: houses});
}
});
});
module.exports = router;
You anticipate that the houses
will be arranged in descending order based on the values of totalPoints
.