在 mongoose 上,有一个不错的选项可以默认使用该select: false
选项从查询中删除一些字段。
例如:
var FileSchema = new Schema({
filename: String,
filesize: Number,
base64Content: {type: String, select:false}
});
[...]
FileModel.find({}, function(err, docs) {
// docs will give me an array of files without theirs content
});
现在,如何对子文档数组的字段使用相同的选项?
(即在以下示例中,设置select: false
为comments
字段)
var PostSchema = new Schema({
user: ObjectId,
content: String,
createdAt: Date,
comments: [{
user: ObjectId,
content: String,
createdAt: Date
}]
});
[...]
FileModel.find({}, function(err, docs) {
// docs will give me an array of files without theirs content
});