Mongoose data saving without _id

前端 未结 1 353
日久生厌
日久生厌 2021-01-18 23:34

I am using mongoose with node.js application. I don\'t want _id field in record. I am using this code to save my record without _id field. But it is giving error

相关标签:
1条回答
  • 2021-01-18 23:45

    Unfortunately, You can not skip having a primary key for the document but you can override the primary key content, you can define your own primary key for each document.

    Try the following schema for the same.

    var PlayerSchema = new mongoose.Schema({
    _id :  { type: Number },
    player_name : { type: String },
    player_age  : { type: Number },
    player_country : { type: String },
    

    } );

    I have replaced your player_id with _id. Now you have control over the primary key of the document and the system won't generate the key for you.

    There are some plugins which can also do the autoincremet for your primary key. https://github.com/chevex-archived/mongoose-auto-increment. You might try these as well.

    Also, about the error you are getting : Any document is an object and should be wrapped inside the curly brackets you can not define two independent object in the same document. So you are getting this error.

    0 讨论(0)
提交回复
热议问题