mongodb - 无法在 Mongo 模式中生成 uuid
问题描述
我正在尝试使用以下代码在我的 Mongodb 4.x 架构中创建一个 id,但我收到一条错误消息,提示未定义 uuid。
_id: { type: String, default: function genUUID() {
uuid.v1()
}}
这看起来像我做的一切都是正确的。
我能错过什么?
我想一个后续问题是,您将如何为架构中的单个值字段自动生成 _id。
例子:
var ProfileSchema = new Schema({
highschool:{
item: { type: String },
_id: { type: String, default: uuid.v4}
},
college:{
item: { type: String },
_id: { type: String, default: uuid.v4}
});
var ProfileSchemaIds = new Schema({
highschool: { type: Schema.Types.ObjectId, ref: 'ProfileSchema.Highschool' },
college: { type: Schema.Types.ObjectId, ref: 'ProfileSchema.College' }
// ... rest of your schema props
});
解决方案
看来您真正需要的是 3 个模型之间的引用,其中Profile
将引用HighSchool
Schema 和College
Schema,如下所示:
var ProfileSchema = new Schema({
highschool: { type: Schema.Types.ObjectId, ref: 'Highschool' },
college: { type: Schema.Types.ObjectId, ref: 'College' }
// ... rest of your schema props
});
Highschool
并且College
将是单独的模式等。
推荐阅读
- python - 如何在正面向后看中使用条件括号?
- ios - Swift didSet 调用但不更新 UILabel - iOS 属性观察者
- r - 删除在超过 3 个组中找到的行
- r - Rmarkdown 中的标头未以所需格式显示
- mysql - 如何将变量和数组中的值从控制器发送到模型
- excel - cell.value 上的类型不匹配
- javascript - 开玩笑:--watch 和--coverage 的结果不同
- python - 具有元刷新的 Python 请求
- rust - 通用特征有界方法与“直接”特征方法的区别
- python - AttributeError:模块“slack”没有属性“RTMClient”