Field "__v" in all documents of a collection

Asked

Viewed 1,976 times

7

All my documents in a Mongodb database have a field __v, what it means?

> db.speeds.find({}).limit(2).pretty()
{
    "_id" : ObjectId("586826f700890738a5e8cb3d"),
    "remoteId" : 1,
    "first" : ObjectId("586826f700890738a5e8cb3a"),
    "second" : ObjectId("586826f700890738a5e8cb3b"),
    "third" : ObjectId("586826f700890738a5e8cb3c"),
    "__v" : 0
}
{
    "_id" : ObjectId("5868270c6a16ce38d79f8af7"),
    "remoteId" : 2,
    "first" : ObjectId("5868270c6a16ce38d79f8af4"),
    "second" : ObjectId("5868270c6a16ce38d79f8af5"),
    "third" : ObjectId("5868270c6a16ce38d79f8af6"),
    "__v" : 0
}
  • You use Mongoose to make the CRUD?

  • Yes, I use Mongoose.

  • I don’t remember what it is exactly, but it has something to do with the version of the record. From a read in this post here: http://stackoverflow.com/questions/12495891/what-is-the-v-field-in-mongodb

1 answer

11


__v is a versionally present key in every document created through the Mongoosis.

This key is incremented when there is a change in the structure of a collection that already has documents, example:

{
    "_id": String,
    "title": String,
    "description": String
}

Changed to:

{
    "_id": String,
    "title": String,
    "description": String,
    "comments": Array
}

Now all documents that are entered will have the versioning key __v: 1.

Setup

It is possible to disable versioning when creating the Schema.

new mongoose.Schema({}, {
    versionKey: false
});

Or change the key name.

new mongoose.Schema({}, {
    versionKey: '_version'
});

http://mongoosejs.com/docs/guide.html#versionKey

Browser other questions tagged

You are not signed in. Login or sign up in order to post.