I see examples all over the place that use both and from what I can see they seem to be the same, but I just don't want to put something in production that breaks because I should use one instead of the other when referencing types like ObjectId and Mixed (both in schema definitions and for creating new object ids with new ObjectId()).
Schemas should always use mongoose.Schema.Types
.
mongoose.Types
are the object you work with within a mongoose document. These are special array subclasses, buffer subclasses, etc that we've hooked into or created special to ease updates and track changes.
@aheckmann can you tell me what will happen if I use mongoose.Types in Schema?
Actually I'm using it for last 2 years in production. But never got any issue.
So before changing the existing system I want to know the internal difference so that changing doesn't hurt anything in production.
@jayadrathamondal using mongoose.Types.ObjectId
for a schema path is fine. For other types, like mongoose.Types.Number
, you'll get an error when you call new Schema()
.
@vkarpov15 I got it. I will change my schema definitions to use mongoose.Schema.Types
Actually I'm using only mongoose.Types.Decimal128
& mongoose.Types.ObjectId
.
For other types I just use system default constructors i.e String
, Number
, Object
.
I think this is the reason I didn't face the errors. Now for testing purpose I have used mongoose.Types.Map
& got the below error.
TypeError: Invalid schema configuration: `MongooseMap` is not a valid type at path `isEmailVerified`. See http://bit.ly/mongoose-schematypes for a list of valid schema types.
Most helpful comment
Schemas should always use
mongoose.Schema.Types
.mongoose.Types
are the object you work with within a mongoose document. These are special array subclasses, buffer subclasses, etc that we've hooked into or created special to ease updates and track changes.