Do all Objects get assigned an ObjectId?

Hi! I’m new to MongoDB, so I don’t know if the following is the correct behavior or not.
I have a collection of games which I imported straight from IGDB’s API. As far as I can see, all objects that are inside an array have been assigned their own ObjectId by MongoDB. Yet, when I insert a new object into an array myself using Shell, no ObjectId is generated. Is this right? Or should MongoDB be assigning a new ObjectId to the object I’m inserting, and for some reason it’s not doing so? An example:

genres: Array (2)

  • 0: Object // Object imported from database
    id: 123
    name: “action”
    _id: ObjectId(‘123456abcdfg’) // ObjectId assigned by MongoDB

  • 1: Object // Object inserted manually
    id: 124
    name: “adventure”
    // No ObjectId

If this is normal, then I can assume the object not having its own ObjectId shouldn’t cause any problems? And if it’s not, why does it happen, and how can I fix it?

Thank you!

Check your IGDBs API if they have _id with ObjectID

Hello,
MongoDB doesn’t automatically assign ObjectIds to embedded documents within arrays. This is to optimize performance. While it might seem counterintuitive, it doesn’t impact basic CRUD operations. If you need unique identifiers for embedded documents, consider adding a custom id field.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.