Gaurav
Gaurav

Reputation: 39

Id is created for every nested objects in documents of a collection in mongodb via mongoose

I have a user schema. While saving document, for every nested object (quizHistory,record & responses) in document, mongoose add _id field automatically. For ref- quizHistory path

const userSchema = new Schema({
    firstName: { type: String, required: true ,trim:true},
    lastName:{ type: String, required: true ,trim:true},
    email: { type: String, unique: true, required: true },
    isUser: { type: Boolean, default: true },
    password: String,
    quizHistory: [{
        quizId: { type: Schema.Types.ObjectId, ref: 'Quiz' },
        record: [{
            recordId:{ type: Number},
            startTime: { type: Date },
            responses: [{
                quesId: { type: Schema.Types.ObjectId, ref: 'Question' },
                answers: [Number]
            }],
            score: Number
        }],
        avgScore: Number
    }]
})

Upvotes: 3

Views: 2242

Answers (1)

Richard Rublev
Richard Rublev

Reputation: 8162

Mongoose create virtual id by default(guide id). Add this line to your schema.

 _id : {id:false}

Upvotes: 2

Related Questions