-->

猫鼬返回文档的只有一个属性,而不是整个文件(Mongoose return only one pro

2019-10-24 05:04发布

猫鼬为我们提供了很多方法( findOnefindfindByID等)找到文件(S)。 所有这些方法返回的整个文件(S)/模型。

是否有可能,当我搜索一个文件,我刚刚从文件/模型返回单个属性,而不是返回整个文档的?

Answer 1:

是,通过设置的投影对象,通常是标准的对象之后指定。

MyModel.find({criteria: 'some criteria'}, {'fieldToInclude': 1, '_id': 0})
                                          ^ Projection object

注: _id总是默认包含的,所以只有它的排斥需要,如果需要指定。



文章来源: Mongoose return only one property of the document and not the entire document