Why new id being created for each array in mongoose

Hi,

I have the following Schema, it looks a bit lengthy but kindly have a look at education, experience and abroad fields

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const ProfileSchema = new Schema({
  user: {
    type: Schema.Types.ObjectId,
    ref: "users"
  },
  handle: {
    type: String,
    required: true,
    max: 40
  },
  company: {
    type: String
  },
  website: {
    type: String
  },
  location: {
    type: String
  },
  status: {
    type: String,
    required: true
  },
  skills: {
      type: [String],
      required: true
  },
  bio: {
    type: String
  },
  githubusername: {
    type: String
  },
  experience: [
    {
      title: {
        type: String,
        required: true
      },
      company: {
        type: String,
        required: true
      },
      location: {
        type: String
      },
      from: {
        type: Date,
        required: true
      },
      to: {
        type: Date
      },
      current: {
        type: Boolean,
        default: false
      },
      description: {
        type: String
      }
    }
  ],
  education: [
    {
      school: {
        type: String,
        required: true
      },
      degree: {
        type: String,
        required: true
      },
      fieldofstudy: {
        type: String,
        required: true
      },
      from: {
        type: Date,
        required: true
      },
      to: {
        type: Date
      },
      current: {
        type: Boolean,
        default: false
      },
      description: {
        type: String
      }
    }
  ],
  social: {
    youtube: {
      type: String
    },
    twitter: {
      type: String
    },
    facebook: {
      type: String
    },
    linkedin: {
      type: String
    },
    instagram: {
      type: String
    }
  },
  date: {
    type: Date,
    default: Date.now
  },
  abroad: [
    {
      country: {
        type: String
      }
    }
]

});

module.exports = Profile = mongoose.model('profile', ProfileSchema);

Now what is happening that when i am creating education, experience or abroad a new id is being created as well, this is happening for fields which i have taken as array, not in other fields, is there something wrong i am doing or is it the way it is

It looks something like below in mongodb

"education": [
        {
            "current": false,
            "_id": "5b46e147a13e32140c69a453",
            "school": "AVS",
            "degree": "MCA",
            "fieldofstudy": "CS",
            "from": "2008-02-04T00:00:00.000Z",
            "to": "2010-02-03T00:00:00.000Z",
            "description": "Study for future job"
        }
    ],

For eg i use below code to add education, same for experience and abroad etc, kindly guide as to why a new id is being generated in mongodb , thanks


router.post(
  '/education',
  passport.authenticate('jwt', { session: false }),
  (req, res) => {
  

    Profile.findOne({ user: req.user.id }).then(profile => {
      const newEdu = {
        school: req.body.school,
        degree: req.body.degree,
        fieldofstudy: req.body.fieldofstudy,
        from: req.body.from,
        to: req.body.to,
        current: req.body.current,
        description: req.body.description
      };

      
      profile.education.unshift(newEdu);

      profile.save().then(profile => res.json(profile));
    });
  }
);

What i am really confused about is , each mongodb document has an id filed , but , why when i am adding education or experience those array are also having id of their own , is it because i am creating a separate route for entering them ?

But then if that is the case, i tested by created a route for country but stored it inside an object and it did not create an id , so why an id when i am trying to store it inside an array which has an object that in turn has objects …

Thanks

Mongo automatically creates an _id field for each member of an array.

1 Like

Thanks, i have never used it before hence was very confused about this behavior

@amitshrivastavafcc but you’re still master of your model, it’s something you can disable by adding a new field config to your education array: _id: false (so that it not be save again).

education: [
    {
      _id: false,
      school: {
        type: String,
        required: true
      },
      degree: {
        type: String,
        required: true
      },
      fieldofstudy: {
        type: String,
        required: true
      },
      from: {
        type: Date,
        required: true
      },
      to: {
        type: Date
      },
      current: {
        type: Boolean,
        default: false
      },
      description: {
        type: String
      }
    }
  ]
1 Like