Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose and partial select/update

In node.js, when I use Mongoose:

is it possible to only fetch some of the values of a large object?

Is it possible to only update some of the values?

like image 584
Yaron Naveh Avatar asked Jul 25 '12 17:07

Yaron Naveh


2 Answers

To fetch only certain fields, pass a string of field names as the second parameter in your find:

// Include the first and last properties, and exclude _id
Model.find({}, 'first last -_id', callback)  

or use the object notation as described here:

Model.find({}, {first: 1, last: 1, _id: 0}, callback)

To only update some of the properties, use an update with a $set modifier:

// Only update the name property
Model.update({_id: 12345}, {$set: {name: 'New name'}}, callback); 
like image 52
JohnnyHK Avatar answered Oct 03 '22 22:10

JohnnyHK


I think with version 3.0.0 this was updated to

Model.find({}, 'first last', callback);

Where first and last are property names on the Model.

like image 23
Paul Avatar answered Oct 03 '22 23:10

Paul