If you wish to use the ObjectId
type for a unique identifier in a Mongoose schema, then you should use Schema.Types.ObjectId
as type
in your schema definition. For example:
import { Schema } from 'mongoose'; const userSchema = new Schema({ id: { type: Schema.Types.ObjectId, required: true }, name: { type: String, required: true }, // ... });
Please note that this is different from how you would specify ObjectId
type in a TypeScript interface.
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.