BranOIE
BranOIE

Reputation: 430

Mongo - Create custom _id

So currently when one of my documents is made, Mongo generates a random ObjectId. However I would like this to be a value of my choosing. I am using postman to test this and if I create a new document and specify a value for _id then it ignores it and overwrites it with the one that Mongo generates.

I am using node to define my schema and I haven't declared an _id field, is that what I must do?

Upvotes: 0

Views: 830

Answers (1)

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

Bydefault Mongodb will generate _id of Type ObjectId , in mongoose if you not define _id field , it will take _id of type ObjectId

if you want to add _id field by your self , you need to define like this in your mongoose model schema

_id: {
    type: Number
  }

Upvotes: 1

Related Questions