无法在Mongo模式中生成uuid

时间:2018-12-06 18:56:46

标签: mongodb mongoose

我正在尝试使用以下代码在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
});

1 个答案:

答案 0 :(得分:1)

您似乎真正需要的是在3个模型之间的引用,其中Profile将引用HighSchool模式和College模式,如下所示:

var ProfileSchema = new Schema({
   highschool: { type: Schema.Types.ObjectId, ref: 'Highschool' },
   college: { type: Schema.Types.ObjectId, ref: 'College' }
   // ... rest of your schema props
});

HighschoolCollege将是独立的架构等。

有关mongoose populate etc的更多信息

相关问题