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?
-
if you don't want it in the result use _doc on the returned object form mongoose– moein rahimiNov 13, 2019 at 7:45
6 Answers
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' })
-
30Is it safe to use this property to determine if a document was just created (e.g.
__v === 0
)? Aug 28, 2013 at 18:24 -
58@ExplosionPills for future reference: no. The version key is only incremented after operations that could cause a conflict, modifying array positions. Other updates won't increment it. The original release post explains it in detail: aaronheckmann.tumblr.com/post/48943525537/… Sep 12, 2013 at 19:04
-
3
-
7@diosney
query.select('-__v')
. @ExplosionPills you would need to add mongoose middleware likeschema.pre('save', function (next) { this.increment(); next(); })
.– wprlDec 20, 2013 at 19:02 -
3@talentedmrjones @wprl That is exactly what I'm doing now, but I wanted something that I could put in the Schema directly so in all queries.– DiosneyMar 24, 2014 at 11:52
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
-
35
-
4You can also call
resultFromMongo.toObject({ versionKey: false })
, to surppress the value. Feb 2, 2017 at 20:08 -
@xperator the outcome is no __V field in the scheme: { "_id": { "$oid": "5aa62e99f36d28237f1a41ad" }, "email": "scott@gmail.com", "sessions": 0 } vs { "_id":{ "$oid": "5aa62e99f36d28237f1a41ad" }, "email": "scott@gmail.com", "sessions": 0, "__v": 0 }– dangMar 12, 2018 at 7:42
-
1
-
1what's the out come after setting to false? other than it not being in the document? May 25, 2020 at 11:54
For remove in NestJS need to add option to Schema()
decorator
@Schema({ versionKey: false })
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
});
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]: Mongoose versioning: when is it safe to disable it?
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();
});
}
}