madu
madu

Reputation: 5460

Finding MongoDB documents with duplicate `_id`

I am running in to a duplicate _id issue while trying to restore the DB dump.

Failed: bulk write error: [{[{can't have multiple _id fields in one document}]},

And want to find out which document it is so I can remove it. I have tried aggregate with:

db.collection.aggregate([
    {"$group" : { "_id": "$_id", "count": { "$sum": 1 } } },
    {"$match": {"_id" :{ "$ne" : null } , "count" : {"$gt": 1} } }
]);

The _id is of the form

{ "_id" : { "$oid" : "55ad085569702d6a820007b3" }

But the aggregate is not giving any result and I am not sure if the statement has an error.

Is it not possible to aggregate on the _id itself? Thank you.

Upvotes: 1

Views: 293

Answers (1)

Joe
Joe

Reputation: 28356

The error "can't have multiple _id fields in one document" is not indicating a duplicate key error where multiple documents have the same value for _id.

It is indicating that a single document has more than one field named _id.

You might try using the bsondump utility to extract the bson files from the dump directory to examine the documents directly.

Upvotes: 3

Related Questions