Field "__v" in all documents in a collection

3

All my documents in a MongoDB database have a __v field, what does it mean?

> 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
}
    
asked by anonymous 31.12.2016 / 23:03

1 answer

3

__v is a version key present in every document created through mongoose .

This key is incremented when a change to the structure of a collection that already has documents occurs, for 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 __v: 1 versioning key.

Configuration

You can disable versioning on Schema creation.

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

Or change the name of the key.

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

link

    
01.01.2017 / 15:24