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
});