Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose retrieving data without _id field

I would like to retrieve some data from a Mongoose setting in my Node.js application. I noticed that no matter what I write as field selection, I always get the _id field. Is there a way not to fetch it? This is how I do right now:

Transaction.find({username : user.username}, ['uniqueId', 'timeout', 'confirmation_link', 'item_name'], function(err, txs){         console.log("user : " + user.username + " with txs: " + txs);         callback(txs); }); 

And logs me the results which contain the _id field.

like image 891
Masiar Avatar asked Mar 07 '12 09:03

Masiar


People also ask

Are Mongoose fields required by default?

It seems that the default value of the required attributes of each field in mongoose schema is false.

What is _ID in Mongoose?

Sep 3, 2019. By default, MongoDB creates an _id property on every document that's of type ObjectId. Many other databases use a numeric id property by default, but in MongoDB and Mongoose, ids are objects by default.

Does Mongoose require schema?

Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.

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.


1 Answers

Another way is to use text argument with prefix - which will exclude this or that field from the result:

Entity.find({ ... }, '-_id field1 field2', function(err, entity) {     console.log(entity);  // { field1: '...', field2: '...' } }); 
like image 142
VisioN Avatar answered Oct 04 '22 20:10

VisioN