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

Deleting documents

In this section, you're going to learn how to delete documents from your MongoDB collections. Before we get into that, in order to explore the methods that let us delete multiple documents or just one, we want to create a few more Todos. Currently, the Todos collection only has two items, and we're going to need a few more in order to play around with all these methods involving deletion.

Now, I do have two. I'm going to go ahead and create a third by right-clicking and then going to Insert Document.... We'll make a new document with a text property equal to something like Eat lunch, and we'll set completed equal to false:

{
   text: 'Eat lunch',
   completed: false
}

Now before we save this, I am going to copy it to the clipboard. We're going to create a few duplicate Todos so we can see how we can delete items based off of specific criteria. In this case, we're going to be deleting multiple Todos with the same text value. I'm going to copy that to the clipboard, click Save, and then I'll create two more with the exact same structure. Now we have three Todos that are identical except for the ID, and we have two that have unique text properties:

Let's go ahead and move into Atom and start writing some code.