mongoose.sort code example
Example 1: mongoose find sort
Post.find({}).sort('test').exec(function(err, docs) { ... });
Post.find({}).sort([['date', -1]]).exec(function(err, docs) { ... });
Post.find({}).sort({test: 1}).exec(function(err, docs) { ... });
Post.find({}, null, {sort: {date: 1}}, function(err, docs) { ... });
Example 2: mongoose select
query.select('a b');
query.select(['a', 'b']);
query.select({ a: 1, b: 1 });
query.select('-c -d');
const schema = new Schema({
foo: { type: String, select: false },
bar: String
});
query.select('+foo');
query.select({ a: 1, b: 1 });
query.select({ c: 0, d: 0 });
Example 3: mongoose where
exports.generateList = function (req, res) {
subcategories
.find({})
.where('categoryId').ne([])
.populate('categoryId')
.where('active').equals(true)
.where('display').equals(true)
.where('categoryId.active').equals(true)
.where('display').in('categoryId').equals(true)
.exec(function (err, data) {
if (err) {
console.log(err);
console.log('error returned');
res.send(500, { error: 'Failed insert' });
}
if (!data) {
res.send(403, { error: 'Authentication Failed' });
}
res.send(200, data);
console.log('success generate List');
});
};