[英]What is the difference between mongoose.Schema() and new mongoose.Schema()?
[英]The same default value gets used every time by `new mongoose.Schema`
我有问题使用new mongoose.Schema
uuid
。 我使用它为设备生成唯一键,并使用Node.js将其保存到MongoDb。 问题是它每次都使用相同的UUID。
这是模型:
const mongoose = require('mongoose');
const uuid = require('uuid/v4');
const DeviceSchema = new mongoose.Schema({
deviceNumberHash: {
type: String,
required: true
},
receivingKey: {
type: String,
default: uuid()
}...
});
知道什么是错的吗?
您正在调用 uuid
并将其返回值作为默认值使用。
相反,传入函数 (通过不在其后put ()
):
const DeviceSchema = new mongoose.Schema({
deviceNumberHash: {
type: String,
required: true
},
receivingKey: {
type: String,
default: uuid // <========== No ()
}...
});
默认值可以是每个文档的一个函数(例如,使用default: Date.now
为日期字段提供默认值)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.