using find in mongoose code example

Example 1: mongoose find() example

// find all documents
await MyModel.find({});

// find all documents named john and at least 18
await MyModel.find({ name: 'john', age: { $gte: 18 } }).exec();

// executes, passing results to callback
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});

// executes, name LIKE john and only selecting the "name" and "friends" fields
await MyModel.find({ name: /john/i }, 'name friends').exec();

// passing options
await MyModel.find({ name: /john/i }, null, { skip: 10 }).exec();

Example 2: mongoose select

// include a and b, exclude other fields
query.select('a b');
// Equivalent syntaxes:
query.select(['a', 'b']);
query.select({ a: 1, b: 1 });

// exclude c and d, include other fields
query.select('-c -d');

// Use `+` to override schema-level `select: false` without making the
// projection inclusive.
const schema = new Schema({
  foo: { type: String, select: false },
  bar: String
});
// ...
query.select('+foo'); // Override foo's `select: false` without excluding `bar`

// or you may use object notation, useful when
// you have keys already prefixed with a "-"
query.select({ a: 1, b: 1 });
query.select({ c: 0, d: 0 });

Example 3: 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([
        // Load travels first
        function(callback) {
            Travel.find({ "date": date }, function (err, travels) {
                if (err) return callback(err);
                locals.travels = travels;
                callback();
            });
        },
        // Load users (won't be called before task 1's "task callback" has been called)
        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) { /* This function gets called after 
          the two tasks have called their "task callbacks" */
        if (err) return next(err);
        //Here locals will be populated with `travellers` and `senders`
        //Just like in the previous example
        console.log(locals);
        console.log(locals.travellers);
        res.json(locals.travellers);
    });
});