为什么Mongoose为对象数组中的每个元素自动创建_id

时间:2019-04-24 10:36:32

标签: mongoose mongoose-schema

我最近创建了一个包含一个由JSON数组组成的字段的Schema,在使用该模式保存我的数据后,我意识到Mongo为该数组的每个元素创建了_id。我知道_id是为mongo中的每个文档创建的。但是是否必须为数组的每个元素都具有_id。有人可以解释吗?

这是我的模式

//@Entity(tableName = "Books_name") 
data class Chapter( 
val can_read: Boolean, 
// autoGenerate = true 
val id_glavs_list: Int, 
val new: Boolean, 
val status: String, 
val title: String 
)

提供给mongo的数据是

const Exams = new Schema({
    course: { type: mongoose.SchemaTypes.ObjectId, ref: 'courses' },
    schedule: [{ 
        subject: {type: mongoose.SchemaTypes.ObjectId, ref: 'subjects'},
        date: { type: Date },
        session: { type: String }
    }]
});

保存到mongo后,这就是我的数据。

{
      "course": "5cbda8a7a2e03c23f674a2bd",
      "schedule": [
        {
          "subject": "5c9f42f8f4dd4f31d75648a0",
          "date": "2019-04-01T18:30:00.000Z",
          "session": "FN"
        },
        {
          "subject": "5c9f430bf4dd4f31d75648a1",
          "date": "2019-04-02T18:30:00.000Z",
          "session": "FN"
        },
        {
          "subject": "5c9f4324f4dd4f31d75648a2",
          "date": "2019-04-24T18:30:00.000Z",
          "session": "FN"
        },
        {
          "subject": "5c9f4331f4dd4f31d75648a3",
          "date": "2019-04-25T18:30:00.000Z",
          "session": "FN"
        },
        {
          "subject": "5c9f4343f4dd4f31d75648a4",
          "date": "2019-04-17T18:30:00.000Z",
          "session": "FN"
        }]
    }

我没有在schedule数组中指定_id,但mongo会自动将其添加。如何避免这种情况?

0 个答案:

没有答案