猫鼬和部分选择/更新

时间:2012-07-25 17:27:22

标签: mongodb mongoose

在node.js中,当我使用Mongoose时:

是否可以只获取大对象的某些值?

是否可以仅更新某些值?

2 个答案:

答案 0 :(得分:12)

要仅提取某些字段,请将一串字段名称作为find中的第二个参数传递:

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

或使用描述为here的对象表示法:

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

要仅更新某些属性,请使用带有$set修饰符的update

// Only update the name property
Model.update({_id: 12345}, {$set: {name: 'New name'}}, callback); 

答案 1 :(得分:3)

我认为3.0.0版更新为

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

其中firstlast是模型上的属性名称。