0

I recently created a Schema with a field that consist of an array of JSON, after using the schema to save my data I realised that Mongo created _id for each element of that array. I know that _id is created for each document in mongo. But is it necessary to have _id for each element of an array. Could anyone explain this ?

This is my schema

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 }
    }]
});

The data provided to mongo is

{
      "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"
        }]
    }

After saving to mongo this is how my data is.

{
    "_id" : ObjectId("5cc03291528b226405341852"),
    "course" : ObjectId("5cbda8a7a2e03c23f674a2bd"),
    "__v" : 0,
    "schedule" : [ 
        {
            "_id" : ObjectId("5cc03629a6b58e1f2ff05aae"),
            "subject" : ObjectId("5c9f42f8f4dd4f31d75648a0"),
            "date" : ISODate("2019-04-01T18:30:00.000Z"),
            "session" : "FN"
        }, 
        {
            "_id" : ObjectId("5cc03629a6b58e1f2ff05aad"),
            "subject" : ObjectId("5c9f430bf4dd4f31d75648a1"),
            "date" : ISODate("2019-04-02T18:30:00.000Z"),
            "session" : "FN"
        }, 
        {
            "_id" : ObjectId("5cc03629a6b58e1f2ff05aac"),
            "subject" : ObjectId("5c9f4324f4dd4f31d75648a2"),
            "date" : ISODate("2019-04-24T18:30:00.000Z"),
            "session" : "FN"
        }, 
        {
            "_id" : ObjectId("5cc03629a6b58e1f2ff05aab"),
            "subject" : ObjectId("5c9f4331f4dd4f31d75648a3"),
            "date" : ISODate("2019-04-25T18:30:00.000Z"),
            "session" : "FN"
        }, 
        {
            "_id" : ObjectId("5cc03629a6b58e1f2ff05aaa"),
            "subject" : ObjectId("5c9f4343f4dd4f31d75648a4"),
            "date" : ISODate("2019-04-17T18:30:00.000Z"),
            "session" : "FN"
        }
    ]
}

I did not specify _id inside the schedule array but mongo add it up automatically. How to avoid this ?

10
  • _id is a mandatory unique key required by the database. If you don't provide one the driver generates it. The only way to avoid it is to use another database. Commented Apr 24, 2019 at 10:39
  • 1
    @Alex … inside an array?! Commented Apr 24, 2019 at 10:41
  • Thanks mate @AlexBlex. But is it applicable for an array inside of a document to ? Commented Apr 24, 2019 at 10:41
  • 3
    It is probably not MongoDB doing this, but Mongoose Commented Apr 24, 2019 at 11:05
  • 2
    ah, sorry, my bad. Didn't pay attention to details. Just add _id:false, to schedule sub-document definition. Commented Apr 24, 2019 at 11:28

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.