Mongoose use of .select() method

Select method is used to select which fields are to be returned in the query result, excluding select means we want all the other fields to be returned, here is simple usage as per the docs.

// include a and b, exclude other fields  
query.select('a b');

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

More information here, https://mongoosejs.com/docs/api.html#query_Query-select


the docs say you can achieve this like so:

Mongoose v4.0

// Retrieving only certain fields

Model.find({}, 'first last', function (err, docs) {

});

old outdated API

// Retrieving only certain fields

Model.find({}, ['first', 'last'], function (err, docs) {
  // docs is an array of partially-`init`d documents
  // defaults are still applied and will be "populated"
});

so you can do this without select().


Now there is a shorter way of doing this (not using .select and not using an array), just passing the fields separate by spaces as the second argument

User.find({}, 'first last', function (err, usr) {
    //Got the result, saved a few bytes of code
});

The Docs


this is another way: queries in mongoose

Transaction.find({username : user.username})
.select('uniqueId confirmation_link item_name timeout username')
.exec(function(err, txs) {
        console.log(txs);
});