Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose __v property - hide?

Tags:

mongoose

Mongoose adds a '__v' property into Schema's for versioning - is it possible to disable this globally or globally hide it from all queries?

like image 937
leepowell Avatar asked Dec 04 '12 09:12

leepowell


People also ask

What does __ V mean in Mongoose?

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 ).

What is versionKey in Mongoose?

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.

What is select in Mongoose?

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.

What is lean Mongoose?

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.


2 Answers

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.

like image 198
theabraham Avatar answered Sep 19 '22 18:09

theabraham


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}}) 
like image 37
Alexander Gonchiy Avatar answered Sep 17 '22 18:09

Alexander Gonchiy