Deleting one or more documents

Command summary (discussed as follows):

  • db.<collection>.deleteOne({<filter>});
  • db.<collection>.deleteMany({<filter>});

As you can see from the command summary, the main focus of delete*() commands is the filter. You use the same syntax as you would when executing find(). Because the delete*() commands can potentially delete all data from a collection if you make a mistake in the filter, it is recommended you use deleteOne() whenever possible.

In this example we delete a customer with the name C. T. Russell:

Before you execute deleteMany(), first run find() to see if the filter you're using yields the correct ...

Get MongoDB 4 Quick Start Guide 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.