Lesson Introduction and Overview

Welcome, future data experts! Today's session explores MongoDB's data deletion. Think of deletion as a space cleanup — removing outdated 'stars' or documents from our 'cosmos' database. Our aim is to learn and master data removal in MongoDB, a crucial step in database management. Let's start our voyage!

Understanding Deletion in MongoDB

Imagine a scenario where a satellite finishes its mission. In our database, its data is no longer relevant. To keep our data up-to-date, we must 'decommission' this satellite's data from our collection. Here in MongoDB, we refer to this removal operation as 'deletion'. It might seem simple, but wielded correctly, deletion becomes a powerful tool in database management.

Precautions in Removing Data

Necessary precautions should be taken before deletion, beginning with a fresh data backup. Deletion can be an irreversible operation, and a backup ensures there's a recovery option in the event of unintended mass deletion.

Deleting Documents Using `deleteOne()` and `deleteMany()`

We use Mongoose's deleteOne() and deleteMany() in MongoDB for deletion. The deleteOne() method deletes the first document matching the condition. Let's pretend we're decommissioning a satellite named 'OldStar' from our Satellite collection:

const mongoose = require('mongoose'); // Importing Mongoose
const Satellite = mongoose.model('Satellite'); // Getting the Satellite collection

// Deleting 'OldStar' satellite document
Satellite.deleteOne({ name: 'OldStar' })
  .then(() => console.log('OldStar has been decommissioned.'))
  .catch((err) => console.error(err));

The deleteMany() method deletes all the documents matching a given condition. Suppose we're decommissioning all inactive satellites:

// Deleting all inactive satellites
Satellite.deleteMany({ isActive: false })
  .then(() => console.log('All inactive satellites have been decommissioned.'))
  .catch((err) => console.error(err));

Ensure you specify conditions accurately to avoid unintended deletions.

Checking Deletion Accuracy with Delete Count

MongoDB's deletedCount property of the deletion response lets us verify the number of documents deleted. This aids in ensuring the accuracy of our operation:

// Deletes all inactive satellites and checks deletion count
Satellite.deleteMany({ isActive: false })
  .then((result) => console.log("Documents deleted: " + result.deletedCount))
  .catch((err) => console.error(err));

Tip: Always verify your operations for accuracy.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal