MongoDB Fundamentals – CRUD: Deleting Objects – Day 5 of 6

This entire week, I am going to discuss the Fundamentals of MongoDB. Today is the fifth post in the six post series. We are going to learn about CRUD Operation: Deleting Objects in MongoDB. I have recently build a Video Course at Foundations of Document Databases with MongoDB focusing on helping users to get started with MongoDB quickly and efficiently.

MongoDB Fundamentals - CRUD: Deleting Objects - Day 5 of 6 DeleteOperation-800x390

CRUD Operation: Delete Operation

CRUD is an acronym and it stands for Create, Read, Update, and Delete. MongoDB is a NoSQL database and that means along with SQL concepts, it is also following NoSQL concepts.

For MongoDB Delete Operations actually stands for modifying and replacing documents from the collection.

Here are the most popular read methods in MongoDB.

db.collection.deleteOne() – Deletes a single document in the collection.

Solarwinds

db.collection.deleteMany() – Deletes multiple document in the collection.

db.collection.remove() – Removes older documents from the collection.

Deleting Objects

MongoDB is schemaless and that makes the delete operation pretty robust most of the time and unpredictable many times as well. MongoDB deletes are case sensitive as well (based on your collation), so you need to also be sure to write your queries for delete operations.

One of the most popular questions, I receive while I work with the delete operation is that does it removes the indexes when the documents are deleted from collections.

Deleting Documents

Let us understand some of the keywords which will help us delete documents from our collection.

We will be using the deleteOne() or deleteMany() methods to delete documents. The very first parameter of the delete operation is the filter (or query). Criteria specified in the filter will qualify the documents from the collections and removes them from the collection.

Here is the usual example of the delete operation.

db.collection.deleteOne(query)

Here are a few sample commands to delete documents from the collections named movie.

Delete all the movies where the move length is equal to 100 minutes.

db.movies.deleteMany( {length: 100} )

Delete only ONE movie where the move length is equal to 100 minutes.

db.movies.deleteOne( {length: 100} )

Remove all the documents where the move length is equal to 100 minutes.

db.movies.remove( {length: 100} )

I hope this example would give you an idea of how we can delete documents (Deleting Objects) inside MongoDB collection.

Learning

Here are some of the relevant blog posts on MongoDB.

Technology Online

I hope you find these Learning paths helpful. If you have a Pluralsight subscription, you can watch it for free. If you do not have a Pluralsight subscription, you can still watch the course for FREE by signing up for a trial account. Please note that you do not need any credit card. You can always connect with me on twitter.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Solarwinds
, , ,
Previous Post
MongoDB Fundamentals – CRUD: Updating Objects – Day 4 of 6
Next Post
MongoDB Fundamentals – Mapping Relational SQL – Day 6 of 6

Related Posts

Leave a Reply

Menu