Search code examples
mongodbmonk

selecting required fields in mongodb using monk


I am able to retrieve only selected fields in mongodb using Monk using the below method

collection.find({}, 'Name Age -_id', function (e, docs) {
    res.json(docs);
});

This works fine, but i am facing issues when i add one more option in the query it throws fn should be a function error as it expects third parameter to be success callback function

I get this error when i try

collection.find({},{limit:5}, 'Name Age -_id', function (e, docs) {
        res.json(docs);
    });

I tried using on success function but still got the same error

collection. find({} ,  { limit: 5 }, 'Name Age -_id').on('success', function (e, docs) {
        res.json(docs);
    });

Solution

  • collection.find({ },  { limit : 5, fields : "Name Age -_id"  },   function (err,data) {
     res.json(docs);
    });