Mongoose adds a '__v' property into Schema's for versioning - is it possible to disable this globally or globally hide it from all queries?
The __v field is called the version key. It describes the internal revision of a document. This __v field is used to track the revisions of a document. By default, its value is zero ( __v:0 ).
The versionKey is a property set on each document when first created by Mongoose. This keys value contains the internal revision of the document. The name of this document property is configurable.
select() is a method of Mongoose that is used to select document fields that are to be returned in the query result. It is used to include or exclude document fields that are returned from a Mongoose query. The select() method performs what is called query projection.
The lean option tells Mongoose to skip hydrating the result documents. This makes queries faster and less memory intensive, but the result documents are plain old JavaScript objects (POJOs), not Mongoose documents.
You can disable the "__v" attribute in your Schema definitions by setting the versionKey
option to false
. For example:
var widgetSchema = new Schema({ ... attributes ... }, { versionKey: false });
I don't think you can globally disable them, but can only do it per Schema. You can read more about Schema's options here. You might also find the Schema set method helpful.
To disable '__v' property, which is not recommended, use the versionKey
schema option:
var Schema = new Schema({...}, { versionKey: false });
To hide it from all queries, which sometimes can be not what you want too, use the select
schema type option:
var Schema = new Schema({ __v: { type: Number, select: false}})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With