cnak2
cnak2

Reputation: 1841

Can't generate a uuid in Mongo schema

I'm trying to create an id in my Mongodb 4.x schema with the following code, but I get an error saying uuid not defined.

_id: { type: String, default: function genUUID() {
    uuid.v1()
}}

This looks like I have everything right.

What can I be missing?

I guess a follow up question is, how would you auto generate an _id for a single value field in your schema.

Example:

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
});

Upvotes: 1

Views: 448

Answers (1)

Akrion
Akrion

Reputation: 18515

It seems what you really need is a reference between the 3 models where Profile would have a reference to HighSchool Schema and to College Schema like this:

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

Highschool and College would be separate schemas etc.

More information on mongoose populate etc

Upvotes: 1

Related Questions