What is the "__v" field in Mongoose

node.jsMongodbMongoose

node.js Problem Overview


I'm using Mongoose version 3 with MongoDB version 2.2. I've noticed a __v field has started appearing in my MongoDB documents. Is it something to do with versioning? How is it used?

node.js Solutions


Solution 1 - node.js

From here:

> 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. The > default is __v.

> If this conflicts with your application you can configure as such:

new Schema({..}, { versionKey: '_somethingElse' })

Solution 2 - node.js

Well, I can't see Tony's solution...so I have to handle it myself...


If you don't need version_key, you can just:

var UserSchema = new mongoose.Schema({
    nickname: String,
    reg_time: {type: Date, default: Date.now}
}, {
    versionKey: false // You should be aware of the outcome after set to false
});

Setting the versionKey to false means the document is no longer versioned.

This is problematic if the document contains an array of subdocuments. One of the subdocuments could be deleted, reducing the size of the array. Later on, another operation could access the subdocument in the array at it's original position.

Since the array is now smaller, it may accidentally access the wrong subdocument in the array.

The versionKey solves this by associating the document with the a versionKey, used by mongoose internally to make sure it accesses the right collection version.

More information can be found at: http://aaronheckmann.blogspot.com/2012/06/mongoose-v3-part-1-versioning.html

Solution 3 - node.js

For remove in NestJS need to add option to Schema() decorator

@Schema({ versionKey: false })

Solution 4 - node.js

It is possible to disable the version key if you don't need it.

See this example:

var User = new mongoose.Schema({
   FullName:{
       type :String,
      
   },
   BirthDay:{
       type :String,
       
   },
   Address:{
       type :String,
   },
   Gender:{
       type:String,
   },
   PhoneNumber:{
       type:Number,
       ref:'Account'
   },
   AccountID:{
        type: Schema.Types.ObjectId,
      	ref: 'Account'
   },
   UserName:{
       type:String,
       ref:'Account'
   }
},{collection:'User',
   versionKey: false //here
});

Solution 5 - node.js

It is the version key.It gets updated whenever a new update is made. I personally don't like to disable it .

Read this solution if you want to know more [1]: https://stackoverflow.com/questions/17810637/mongoose-versioning-when-is-it-safe-to-disable-it

Solution 6 - node.js

We can use versionKey: false in Schema definition

'use strict';

const mongoose = require('mongoose');

export class Account extends mongoose.Schema {

    constructor(manager) {

        var trans = {
            tran_date: Date,
            particulars: String,
            debit: Number,
            credit: Number,
            balance: Number
        }

        super({
            account_number: Number,
            account_name: String,
            ifsc_code: String,
            password: String,
            currency: String,
            balance: Number,
            beneficiaries: Array,
            transaction: [trans]
        }, {
            versionKey: false // set to false then it wont create in mongodb
        });

        this.pre('remove', function(next) {
            manager
                .getModel(BENEFICIARY_MODEL)
                .remove({
                    _id: {
                        $in: this.beneficiaries
                    }
                })
                .exec();
            next();
        });
    }

}

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
QuestionSimon LomaxView Question on Stackoverflow
Solution 1 - node.jsTony The LionView Answer on Stackoverflow
Solution 2 - node.jskenberkeleyView Answer on Stackoverflow
Solution 3 - node.jsLysakView Answer on Stackoverflow
Solution 4 - node.jsNguyen PhuView Answer on Stackoverflow
Solution 5 - node.jsMuhammed shakir CKView Answer on Stackoverflow
Solution 6 - node.jsKARTHIKEYAN.AView Answer on Stackoverflow