node.js 猫鼬中的“__v”字段是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12495891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:19:18  来源:igfitidea点击:

What is the "__v" field in Mongoose

node.jsmongodbmongoose

提问by Simon Lomax

I'm using Mongooseversion 3 with MongoDBversion 2.2. I've noticed a __vfield has started appearing in my MongoDBdocuments. Is it something to do with versioning? How is it used?

我正在使用Mongoose版本 3 和MongoDB版本 2.2。我注意到一个__v字段已开始出现在我的MongoDB文档中。它与版本控制有关吗?它是如何使用的?

采纳答案by Tony The Lion

From here:

这里

The versionKeyis 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:

versionKey是 Mongoose 首次创建时在每个文档上设置的属性。此键值包含文档的内部修订。此文档属性的名称是可配置的。默认值为__v.

如果这与您的应用程序冲突,您可以这样配置:

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

回答by kenberkeley

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:

如果您不需要 version_key,您可以:

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

回答by KARTHIKEYAN.A

We can use versionKey: falsein Schema definition

我们可以在 Schema 定义中使用versionKey: false

'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();
        });
    }

}