Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose use of .select() method

I'm pretty confused with the use of the select method. This is how I use it, and it's wrong:

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

What I'm trying to achieve is simply to select from the transactions in the database the ones with that username and I want to take out just the fields listed in the select method. Can anyone point out how should I use the select method? Thanks.

like image 909
Masiar Avatar asked Oct 20 '22 18:10

Masiar


People also ask

What does find () return in Mongoose?

find() function returns an instance of Mongoose's Query class. The Query class represents a raw CRUD operation that you may send to MongoDB. It provides a chainable interface for building up more sophisticated queries. You don't instantiate a Query directly, Customer.

What is done () in Mongoose?

It just means that your new document will have a field with "done" as key and "false" boolean as value.

What is $in in Mongoose?

The value of the $in operator is an array that contains few values. The document will be matched where the value of the breed field matches any one of the values inside the array.

What is Mongoose query?

The Mongoose Query class provides a chaining interface for finding, updating, and deleting documents.


1 Answers

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().

like image 91
Philipp Kyeck Avatar answered Oct 22 '22 07:10

Philipp Kyeck