Ignore mongodb Aggregate errors and just return the error proned _ids

How can I tell mongodb to ignore the E11000 duplicate key error while I am querying an aggregate and just return their _id?

More info:

  • I am using $merge operator.
  • I am using MongoDB v5.x
  • I am mocing data from collection-a to a new collection (let’s name it collection-b)
  • There are 100,000 documents in collection-a and I need to break it into 3 different new collection.

Are you expecting the documents in the new collections to have the same _id values as the existing documents? If not, you could let mongo create new _id values and store the old (sometimes not unique) _ids values in another property that is not uniquely indexed.

1 Like

Sorry for my writing a question with low information. Assume we have users collection with these fields: _id, name, address, phoneNumber, age. Now I wanna copy part of my users’ info - Just name and phoneNumber - into another collection named test. In the users collection the phoneNumber field is not indexed (unique).

In the test collection I decided to index my phoneNumber. But now I want to ignore the E11000 duplicate key error on the phoneNumber field and skip those docs with duplicated phoneNumber.

BTW now I figured I have to distinct the users based on their phoneNumber first, then copy them into another collection.

What do you think? Is it good or there are other solutions too?