Mongoose virtual fields included in toJSON by default: schemaOptions.toJSON.virtuals = true; still doesn't include virtual fields by default

mongoose virtual populate
mongoose virtual not working
mongoose async virtual
mongoose virtual undefined
mongoose schema methods
mongoose json schema
mongoose schema timestamps
mongoose schema _id

I saw in another answer that in order to include the virtual fields you must do like https://groups.google.com/forum/?fromgroups#!topic/mongoose-orm/HjrPAP_WXYs

var schemaOptions = {
  toJSON: {
    virtuals: true
  }
};

which I've done;

Now in the Schema:

 new Schema({...}, schemaOptions);

But still so, the data doesn't include the virtual.. :s

But like this works:

var docsCallback = function(err, docs){
    var i = docs.length;
    var nDocs = [];
    while(i--){
        nDocs[i] = docs[i].toObject({virtuals: true});
    }
    done(nDocs);
}

Just tried:

  var schemaOptions = {
    toObject: {
      virtuals: true
    }
  };

and worked! ;)

Now by default I use:

  var schemaOptions = {
    toObject: {
      virtuals: true
    }
    ,toJSON: {
      virtuals: true
    }
  };

Virtual attributes, Query Streams · Methods & Statics · Defaults · Indexes · Validation · Virtuals · Getters / Setters · Embedded Docs var PersonSchema = new Schema({ name: { first: String , last: String } }); If you want the virtual field to be displayed on client side, then set {virtuals: true} for toObject and toJSON in schema options as below: If you use toJSON() or toObject() mongoose will not include virtuals by default. This includes the output of calling JSON.stringify() on a Mongoose document, because JSON.stringify() calls toJSON() .

You can do this way as well:

docs.set('toJSON', { virtuals: true });

Mongoose v5.9.18: Mongoose Tutorials: Mongoose Virtuals, The opposite to hide would be nice, but doesn't belong to mongoose in my opinion. Implemented a way to hide fields by default when calling toJSON on a document #1160 toJSON() call, the properties will still be accessible from your document like usual mongoose.plugin(function (schema, options) { schema.​options. 53 Mongoose virtual fields included in toJSON by default: schemaOptions.toJSON.virtuals = true; still doesn't include virtual fields by default 49 Why is null in JavaScript bigger than -1, less than 1, but not equal (==) to 0?

For me it worked only after adding getters: true to schema options, as mentioned in mongoose docs, i.e.

var schemaOptions = { toObject: { getters: true }, toJSON: { getters: true } };

Mongoose v5.9.18: Schemas, I have set these schema options. works perfectly and populate with virtual fields. When virtuals: true }); var Resource = mongoose.model('Resource', node gh-4377.js (node:14686) DeprecationWarning: Mongoose: mpromise (​mongoose's default Please modify the script if this is still an issue for you  53 Mongoose virtual fields included in toJSON by default: schemaOptions.toJSON.virtuals = true; still doesn't include virtual fields by default 49 Why is null in JavaScript bigger than -1, less than 1, but not equal (==) to 0?

Allow hiding schema fields when calling toJSON · Issue #1020 , Each schema maps to a MongoDB collection and defines the shape of the documents record is converted to an object or JSON, virtuals are not included by default. Pass virtuals : true to either toObject() or to toJSON() to have them returned. Mongoose assigns each of your schemas an _id field by default if one is not  Mongoose assigns each of your schemas an id virtual getter by default which returns the documents _id field cast to a string, or in the case of ObjectIds, its hexString. If you don't want an id getter added to your schema, you may disable it passing this option at schema construction time.

populate doesn't include virtuals when populate multiple documents , Missing Mongoose Schema Property Still Being Returned - node.js. still exists I thought that if the schema didn't specify a property then it wouldn't If you insert a field not declared in the schema from node.js you will get a 400 Bad request. c. toJSON doesn't include virtuals, so when you send a mongoose document to  By default mongoose doesn’t output virtuals, to archive this you need to add toObject and(/or) toObject to schemaOptions in @modelOptions Note: it can be set in @modelOptions , but it can be set in getModelForClass too (and in the doc.toJSON() / doc.toObject() functions)

Mongoose Schemas v4.4.14, Mongoose virtual fields included in toJSON by default: schemaOptions.toJSON.​virtuals = true; still doesn't include virtual fields by default. 53 Mongoose virtual fields included in toJSON by default: schemaOptions.toJSON.virtuals = true; still doesn't include virtual fields by default 49 Why is null in JavaScript bigger than -1, less than 1, but not equal (==) to 0?

Comments
  • Does toObject automatically .populate any refs found in the Object? Let's say a Post object that has a User ref in it. @Totty.js
  • just accept your own answer so the question shows as answered in the question list