本文介绍了猫鼬和部分选择/更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在node.js中,当我使用猫鼬时:
In node.js, when I use Mongoose:
是否只能获取一个大对象的某些值?
is it possible to only fetch some of the values of a large object?
是否只能更新某些值?
推荐答案
仅获取某些字段,请将一串字段名称作为find
中的第二个参数传递:
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)
要仅更新某些属性,请使用update
和 $set
修饰符:
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);
这篇关于猫鼬和部分选择/更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!