- Advanced Node.js Development
- Andrew Mead
- 230字
- 2021-08-27 19:05:58
Running the findOneAndDelete and deleteMany statements
Let's go ahead and comment out findOneAndDelete first. We'll run the deleteMany statement. Over in the Terminal, I can shut down the current connection, start it up again, and if we go over to Robomongo, we should see that those three documents were deleted. I'm going to right-click on Users and view the documents:
We just get the two documents back. Anything where the name was Andrew is now removed, which means our statement worked as expected, and this is fantastic.
Next up, we can run our findOneAndDelete statement. In this case, we're expecting that that one document, the one where the name equals Mike, gets removed. I'm going to go ahead and make sure I save the file. Once I do, I can move into the Terminal and rerun the script. This time around, we get the document back where the name is Mike. We did target the correct one, and it does appear that one item was deleted:
I can always go ahead and verify this by refreshing the collection inside of Robomongo:
I get my collection with just one document inside of it. We are now done. We know how to delete documents from our MongoDB collections; we can delete multiple documents; we can target just one, or we can target one and get its value back.