Mongoose retrieving data without _id field


Question

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.

1
44
3/7/2012 9:08:51 AM

Accepted Answer

_id must be specifically excluded. For example,

Transaction.find({username : user.username}, { '_id': 0, 'uniqueId' :1, 'timeout': 1, 'confirmation_link': 1, 'item_name': 1}, function(err, txs){
  console.log("user : " + user.username + " with txs: " + txs);
  callback(txs);
});
56
3/7/2012 11:48:52 AM

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: '...' }
});

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon