Advanced Node.js Development
上QQ阅读APP看书,第一时间看更新

Using the deleteMany and findOneAndDelete methods

We're going to go ahead and go over a quick challenge to test your skills. Inside of Robomongo, we can look at the data we have in the Users collection. I'm going to open it up, highlight all the data, and expand it recursively so we can view it:

We have the name Jen; we have Mike; we have Andrew, Andrew and Andrew. This is perfect data. Yours might look a little different, but the goal is to use two methods. First up, look for any duplicates, anything that has a name set to the name of another document. In this case, I have three documents where the name is Andrew. What I want to do is use deleteMany to target all of these documents and remove them. I also want to use findOneAndDelete to delete another document; it doesn't matter which one. And I want you to delete it by ID.

In the end, both statements should show their effect over inside of Robomongo. When I'm done, I'm hoping to see these three documents deleted. They all have the name Andrew, and I'm hoping to see the document where the name Mike is deleted, because I'm going to target this one by ID in my findOneAndDelete method call.

First up, I'm going to write my scripts, one for deleting users where the name is Andrew and one for deleting the document with the ID. In order to grab the ID, I am going to go ahead and edit it and simply grab the text inside of quotes, and then I can cancel the update and move into Atom.