In my database I have two collections, but some documents were added (possibly) at different times to both collections.
I could use mongodump
and then mongorestore
for merging. But then I have the same documents that were added to both collections as duplicates in my new collection. mongorestore --drop
does not help neither, because the documents not necessarily have the same _id
.
How to drop a document when a document with userid
exists already?
Best Answer
There might be more elegent solutions for this, but let me answer how I merged a new collection into an original collection without importing the duplicates.
userid
fields in both collections and take the difference.$out
operator to save documents to a difference collection. This new collection has all documents of the new collection, except of the duplicates.mongodump
the difference collection andmongorestore
it into the original collection.