【问题标题】:Id is created for every nested objects in documents of a collection in mongodb via mongoose通过 mongoose 为 mongodb 中集合的文档中的每个嵌套对象创建 ID
【发布时间】:2021-01-13 14:57:57
【问题描述】:

我有一个用户架构。在保存文档时,对于文档中的每个嵌套对象(quizHistory、record 和响应),猫鼬会自动添加 _id 字段。 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
    }]
})

【问题讨论】:

    标签: mongodb mongoose nosql mongoose-schema


    【解决方案1】:

    Mongoose 默认创建虚拟 id(guide id)。 将此行添加到您的架构中。

     _id : {id:false}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 2022-01-09
      相关资源
      最近更新 更多