Mongoose __v property - hide?

Mongoose

Mongoose Problem Overview


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

Mongoose Solutions


Solution 1 - Mongoose

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.

Solution 2 - Mongoose

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

Solution 3 - Mongoose

Two ways:

  1. {versionKey: false}

  2. when you query, like model.findById(id).select('-__v')

'-' means exclude the field

Solution 4 - Mongoose

define a toObject.transform function, and make sure you always call toObject when getting documents from mongoose.

var SomeSchema = new Schema({
    <some schema spec>
  } , {
    toObject: {
      transform: function (doc, ret, game) {
        delete ret.__v;
      }
    }
});

Solution 5 - Mongoose

Try this it will remove _v from every query response.

// transform for sending as json
function omitPrivate(doc, obj) {
    delete obj.__v;
    return obj;
}

// schema options
var options = {
    toJSON: {
        transform: omitPrivate
    }
};
// schema
var Schema = new Schema({...}, options);

Solution 6 - Mongoose

You may not want to disable __v, other answers provide few links to answer why you shouldn't disable it.

I've used this library to hide the __v and _id

https://www.npmjs.com/package/mongoose-hidden

let mongooseHidden = require("mongoose-hidden")();

// This will add `id` in toJSON
yourSchema.set("toJSON", {
        virtuals: true,
    });

// This will remove `_id` and `__v` 
yourSchema.plugin(mongooseHidden);

Now __v will exist, but it won't be returned with doc.toJSON().

Hope it helps.

Solution 7 - Mongoose

You can use a Query Middleware to exclude any field from the output. In your case you can use this:

// '/^find/' is a regex that matches queries that start with find
// like find, findOne, findOneAndDelete, findOneAndRemove, findOneAndUpdate
schema.pre(/^find/, function(next) {
    // this keyword refers to the current query
    // select method excludes or includes fields using + and -
    this.select("-__v");
    next();
});

For more information in docs lookup: Middlewares select method

Solution 8 - Mongoose

Yes, it is simple, just edit the "schema.js" file that is inside

"node_modules\mongoose\lib"

Search for "options = utils.options ({ ... versionKey: '__v'..." and change value "__v" to false.

This will change all post requests. (versionKey: '__v' => versionKey: false)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleepowellView Question on Stackoverflow
Solution 1 - MongoosetheabrahamView Answer on Stackoverflow
Solution 2 - MongooseAlexander GonchiyView Answer on Stackoverflow
Solution 3 - MongooseEarthShakerView Answer on Stackoverflow
Solution 4 - MongooseAmit PortnoyView Answer on Stackoverflow
Solution 5 - MongooseSimranView Answer on Stackoverflow
Solution 6 - MongooseAshwani AgarwalView Answer on Stackoverflow
Solution 7 - MongooseMiad AbdiView Answer on Stackoverflow
Solution 8 - MongooseLeandro SantanaView Answer on Stackoverflow