版本`__v`没有出现在文档

时间:2016-09-06 07:03:31

标签: mongodb mongoose mongoose-schema

我的架构中没有__v。我已经读过,默认情况下它应该出现在所有文件中。我甚至试图通过设置options = {versionKey: true}强制它。

这是我的架构:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const options = {
    versionKey: true
};

const schema = new Schema({
    _id: { type: String, unique: true, required: true },
    ports_server: Array
}, options);

const Host = mongoose.model('Host', schema);

module.exports = Host;

我错过了什么?

1 个答案:

答案 0 :(得分:1)

__v没有显示,因为我 upserting 我的文档。而不是Host.update(..)Host.create(..)我必须new Host(..).save()

仅提供1个文档的正确方法是

    Host.findByIdAndUpdate(
        host._id,
        host,
        {upsert: true},
        callback
    );