If I define a model in mongoose, and create a document using something like the code below:
const Model = require("./Model")
const newModelItem = new Model({
...data
})
await newModelItem.save()
I noticed that there is an ID field immediately available in the newModelItem object.
How does MongoDB ensure that the key isn't a duplicate?

ObjectIdyou have 16 Million unique ID's which can be generated per second / per process. That should be sufficient for real life.