Set select: false to subdocuments array at mongoose

mongodb, mongoose, node.js, schema, subdocument

Solution

Try creating a CommentSchema first,

var CommentSchema = new Schema({
  user: ObjectId,
  content: String
  //whatever else
});

and then in your PostSchema specify

comments: { type: [CommentSchema], select:false}

Problem

On mongoose there is a nice option to remove some fields from queries by default using the `select: false` option. For Example: ``` 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 }); ``` Now, how can I use the same option to a field of subdocuments array? (ie. in the following example, set `select: false` to the `comments` field) ``` 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 }); ```

Original source