How to override _id in mongoose

  Kiến thức lập trình

I have a schema like this:

const sessionCardSchema = new Schema(
    {
        unit: Number,
        creator: String
    }
);

I can save a card like this:

const Card = {
  unit: 2,
  creator: 'Jack'
}
const sessionCard = new db.SessionCard(Card);
await sessionCardModel.save(); 

The issue is I cannot save my own objectId as _id of the document.

Here is what I tried:

My schema:

Note: with or without _id in the schema it doesn’t work.

const sessionCardSchema = new Schema(
    {
        
        // _id: mongoose.Schema.Types.ObjectId,
        unit: Number,
        creator: String

    }, { _id: false }
);

My code:

const Card = {
  _id: '657c95a4b6e31b1c968aec94'
  unit: 2,
  creator: 'Jack'
}
const sessionCard = new db.SessionCard(Card);
await sessionCardModel.save(); 

2

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT