Missing _id in one element of array


I’ve encountered a strange behavior that I can’t explain nor reproduce.
There are documents in my db that contain an array with either null element or element that does not have an _id.

I event don’t know how to reproduce something like this. I see no way to insert a null element, remove _id from existing element or insert a new element without _id.

How is this kind of data corruption possible?

No it is not, unless you uncover a major bug. But that is a very unlikely.

Please share the documents that you think are not correct.

Note that the only mandatory _id is the one at the top level, if you do not supply one, it is automatically added. It cannot be removed or modified.

Are you using mongoose or schema validation?

It happened again!
Here is one of such documents and a scheme for it.
I did manage to produce a document with null entry in an array.
You can do this by finding a document, lean it, remove one array entry in the object and then update using this object.
But how to remove the _id ?


const Schema = mongoose.Schema({
  _id: {
     type: Number,
  items: [
      eid: String,
      no: Number,
      exp: Date,
  skin: [
      type: String,

In plain JSON your document is a valid.

With mongoose, it is a different beast. I do not know mongoose. I avoid obsabstruction layers.

I think I have seen a way to make a field mandatory in mongoose but I do not remember how. What is funny is that you have the field _id in your items but it is not part of your schema. May be your documents have been added directly in mongosh or Compass.

I added the tag mongoose to your post in case someone with that specific knowledge can help you.

Hi @Seweryn_Panczyniak welcome to the community!

Yes I agree with @steevej that this may be a mongoose-specific issue.

However the _id field for the sub-documents in the array is different from the top-level _id: 1234. This looks like the content of the array was referencing some other collection. Could you maybe post a small example that you know can reproduce this? My goal is to be able to run your code example in my PC and recreate what you’re seeing here.

Also, you might want to raise an issue in the official mongoose github page describing what happened.

Best regards

Maybe it is mongoose fault. As I understand the _id in object of an array is mandatory and automatically added by driver. I was thinking that this is by default behavior of mongodb as there is no way to remove the _id from such element in MongoDB Compass.
Just as I’ve wrote the above line, I’ve double check it in Robo 3T an it was able to remove the _id!
Now that I know that it is possible to create such a document I will go thru my code again.
Thank you guys!


I was really surprised to read

because the _id within the objects of the items array have no special signification except that by convention we (at least I do) use _id to make it clear I refer to a top level document.

So I fired Compass and indeed, the GUI does not present the little X for delete when the field name is _id no matter where it is in the structure of the documents. We even cannot edit the value. It is the same with cloud.mongodb.com. I understand why it is not possible for the top _id. But why the others?

Is that a bug or feature?

Luckily, we can remove and modify inner fields named _id with $set.

1 Like

Yes this is a known issue in Compass. This is the relevant ticket: COMPASS-6160

Glad to see it all works out!

Thanks @steevej for checking this out :+1:

Best regards

1 Like