Want Mongodb to generate _id automatically for array elements?

The great MongoDB doesn’t generate _id for array elements automatically unless it’s schema is defined separately. The below is the example to define a separate schema for the subdocuments.

With the above Schema, MongoDB will generate _id for every element you insert into the “children” array.

Having _id for array elements makes it easy to find, delete and update the same. Otherwise, they have to be found by the index which will not be the same as always.

Leave a Reply

Your email address will not be published. Required fields are marked *