mongoose.find(query) code example
Example 1: 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 2: mongoose query with function(req, res, next)
router.post('/travellers',
passport.authenticate('jwt', { "session": false }), function(req, res, next) {
var pickup_location = req.body.pickup_location;
var delivery_location = req.body.delivery_location;
var date = req.body.date;
var sender = req.user._id;
var locals = {
travellers: [],
senders: []
};
async.series([
function(callback) {
Travel.find({ "date": date }, function (err, travels) {
if (err) return callback(err);
locals.travels = travels;
callback();
});
},
function(callback) {
async.forEach(locals.travels, function (travel, callback) {
User.findById(travel.traveller, function (err, user) {
if (err) return callback(err);
data = {
"name": user.name,
"email": user.email,
"phone": user.phone,
"image_url": user.image_url,
"type": "traveller"
};
console.log(data);
local.travellers.push(data);
callback();
});
}, function (err) {
if (err) return callback(err);
callback();
});
}
], function(err) {
if (err) return next(err);
console.log(locals);
console.log(locals.travellers);
res.json(locals.travellers);
});
});