我使用Mongoose版本3和MongoDB版本2.2。我注意到一个__v字段已经开始出现在我的MongoDB文档。这与版本控制有关吗?它是如何使用的?
当前回答
__v字段称为版本键。它描述了文档的内部修订。__v字段用于跟踪文档的修订。默认值为0 (__v:0)。
如果你不想使用这个版本键,你可以像mongoose一样使用versionKey: false。模式参数。
你可以按照这个例子…
const mongoose = require('mongoose');
const userSchema = mongoose.Schema(
{
name: {
type: String,
require: true
},
email: {
type: String,
unique: true
},
password: {
type: String,
}
},
{
timestamps: true,
versionKey: false, // Here You have to add.
}
)
module.exports = mongoose.model('tbl_user', userSchema)
其他回答
它是版本键。每当有新的更新时,它就会更新。我个人不喜欢禁用它。
如果你想了解更多,请阅读这个解决方案 [1]:猫鼬版本:什么时候关闭它是安全的?
我们可以在模式定义中使用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();
});
}
}
在NestJS中删除需要添加选项Schema()装饰器
@Schema({ versionKey: false })
__v字段称为版本键。它描述了文档的内部修订。__v字段用于跟踪文档的修订。默认值为0 (__v:0)。
如果你不想使用这个版本键,你可以像mongoose一样使用versionKey: false。模式参数。
你可以按照这个例子…
const mongoose = require('mongoose');
const userSchema = mongoose.Schema(
{
name: {
type: String,
require: true
},
email: {
type: String,
unique: true
},
password: {
type: String,
}
},
{
timestamps: true,
versionKey: false, // Here You have to add.
}
)
module.exports = mongoose.model('tbl_user', userSchema)
我看不出托尼的解决方案……所以我得自己处理
如果你不需要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
});
将versionKey设置为false意味着文档不再有版本。
如果文档包含一组子文档,这就会出现问题。可以删除其中一个子文档,从而减小数组的大小。稍后,另一个操作可以在数组中的原始位置访问子文档。
由于数组现在变小了,它可能会意外地访问数组中错误的子文档。
versionKey通过将文档与mongoose内部使用的a versionKey相关联来解决这个问题,以确保它访问正确的集合版本。
更多信息请访问:http://aaronheckmann.blogspot.com/2012/06/mongoose-v3-part-1-versioning.html
推荐文章
- 如何排序mongodb与pymongo
- 如何在mongodb上导入。bson文件格式
- 很好的初学者教程socket.io?
- CALL_AND_RETRY_LAST分配失败-进程内存不足
- 在Ubuntu上安装Node.js
- 使用express.js代理
- JSON文件的蒙古导入
- Node -使用NODE_MODULE_VERSION 51根据不同的Node.js版本编译
- 如何删除mongodb中的数组元素?
- RabbitMQ / AMQP:单队列,同一消息的多个消费者?
- 修改MongoDB数据存储目录
- Node.js同步执行系统命令
- 在MongoDB中查找重复的记录
- 禁用包的postinstall脚本
- 为什么MongoDB Java驱动在条件中使用随机数生成器?