MongoDB Tutorial

  • Home
  • Getting Started
  • CRUD
  • Indexes
Home / MongoDB CRUD / MongoDB deleteOne

MongoDB deleteOne

Summary: in this tutorial, you’ll learn how to use the MongoDB deleteOne() method to delete a single document from a collection.

Introduction to the MongoDB deleteOn() method

The deleteOne() method allows you to delete a single document from a collection.

The deleteOne() method has the following syntax:

db.collection.deleteOne(filter, option)

The deleteOne() method accepts two arguments:

  • filter is an required argument. The filter is a document that specifies the condition for matching the document to remove. If you pass an empty document {} into the method, it’ll delete the first document in the collection.
  • option is an optional argument. The option is a document that specifies the deletion options.

The deleteOne() method returns a document containing the deleteCount field that stores the number of deleted documents.

To delete all documents that match a condition from a collection, you use the deleteMany() method.

MongoDB deleteCount() method examples

We’ll use the following products collection:

db.products.insertMany([ { "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256]}, { "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512]}, { "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128]}, { "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024]}, { "_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]} ])

1) Using the deleteOne() method to delete a single document

The following example uses the deleteOne() method to delete a document from the products collection where the _id is 1:

db.products.deleteOne({ _id: 1 })

The query returned the following document:

{ "acknowledged" : true, "deletedCount" : 1 }

The deleteCount field returned 1 indicating the number of deleted documents.

2) Using the deleteOne() method to delete the first document from a collection

The following query uses the deleteOne() method to remove the first document returned from the products collection:

db.products.deleteOne({})

It returned the following document:

{ "acknowledged" : true, "deletedCount" : 1 }

In this example, we passed an empty document {} to the deleteOne() method, therefore, it removed the first document from the products collection.

Summary

  • Use the MongoDB deleteOne() method to remove a single document that matches a condition.
  • Pass an empty document into the deleteOne() method to delete the first document from the collection.
  • Was this tutorial helpful ?
  • YesNo
Previous MongoDB Upsert
Next MongoDB deleteMany

Getting Started

  • What is MongoDB
  • Install MongoDB
  • MongoDB Basics
  • MongoDB Shell
  • MongoDB Data Types

Inserting Documents

  • insertOne
  • insertMany
  • mongoimport

Selecting Documents

  • findOne
  • find
  • Projection: Selecting Returned Fields

Comparison Query Operators

  • $eq: Equal To Operator
  • $lt: Less Than Operator
  • $lte: Less Than or Equal To Operator
  • $gt: Greater Than Operator
  • $gte: Greater Than or Equal To Operator
  • $ne: Not Equal To Operator
  • $in: In Operator
  • $nin: Not In Operator

Logical Query Operators

  • $and: Logical AND Opeartor
  • $or: Logical OR Operator
  • $not: Logical NOT Operator
  • $nor: Logical NOR Operator

Element Query Operators

  • $exists
  • $type

Array Query Operators

  • $size
  • $all
  • $elemMatch

Sorting & Limiting

  • sort(): Sorting documents
  • limit(): Limiting documents

Updating Documents

  • updateOne: Update one Document
  • updateMany: Update Multiple Documents
  • $inc: Increase / Decrease Field Value
  • $min: Update Field Value
  • $max: Update Field Value
  • $mul: Mutiply Field By a Number
  • $unset: Remove Fields
  • $rename: Rename Fields
  • Upsert

Deleting Documents

  • deleteOne
  • deleteMany

About MongoDBTutorial.com

This MongoDB Tutorial helps you master MongoDB quickly.

Recent Tutorials

  • MongoDB Indexes
  • MongoDB limit
  • MongoDB sort
  • MongoDB Upsert
  • MongoDB Projection

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Copyright © 2021 ยท By mongodbtutorial.org. All Rights Reserved.