Closed sanjeevrouhan closed 2 years ago
Sorry, I do not use Mangoose to help you here.
Every ID is very unique with default settings. There could be done problem with Mangoose config.
Hi @sanjeevrouhan
Had the same problem, turns out you would have to write it as lambda:
_id: { type: String, default: () => nanoid() }
If you don't, it will create the same id over and over..
Showing error
E11000 duplicate key error collection: mongodb_example.users index: _id_ dup key: { _id: \"RXtVE_W9hEAVg8c6Os1wO\" }
Here is my Model code used for _id
`import { nanoid } from "nanoid"; import { Schema, model } from "mongoose";
const UserSchema = new Schema({ _id: { type: String, default: nanoid(), }, firstName: { type: String, required: true, }, lastName: { type: String, required: false, }, email: { type: String, required: true, }, password: { type: String, required: true, }, }); `