Removing duplicate documents

First up, we're going to go ahead and try to remove the duplicate users, and I'm going to do this by using db.collection. We're going to target the Users collection, and in this particular case, we're going to be using the deleteMany method. Here, we're going to try to delete all of the users where the name property equals Andrew.

db.collection('Users').deleteMany({name: 'Andrew'});

Now I could tack on a then call to check for success or errors, or I could just leave it like this, which is what I'm going to do. If you use a callback or the promise then method, that is perfectly fine. As long as the deletion happens, you're good to go.

Get Advanced Node.js Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.