Deleting items by ID in MongoDB
Sometimes we need to delete data from a database based on specified criteria. Unlike other SQL databases, MongoDB does not include SQL queries for this purpose. Instead, it uses commands.
This article will discuss how to delete documents based on certain conditions like ID etc. Also, we will look at an example with explanations on the topic to make it easier.
Deleting a document by ID in MongoDB
MongoDB includes a deleteOne()
built-in method called for performing delete operations. This function takes various criteria as parameters.
The general syntax of this method is as follows.
db.Your_Collection.deleteOne({ Your_Criteria_Here })
Since we are willing to delete documents based on their IDs, we can update the syntax as follows:
db.mycollection.deleteOne( {"_id": ObjectId("Your_ID_Here")});
Now, let's look at an example related to this topic.
In the following example, we will illustrate how to delete a document based on a specified ID. To do this, let's first create some documents in the collection.
{
"_id": ObjectId("6371fd850f19826ee6ca5139")
"sl": 1,
"Name": "Alex"
}
{ _id: ObjectId("6371fd850f19826ee6ca5138"),
sl: 0,
Name: 'Alen'
}
Now, let's delete a document with ID 6371fd850f19826ee6ca5139. To do this, we will use the following command
db.mycollection.deleteOne( {"_id": ObjectId("6371fd850f19826ee6ca5139")});
After executing the above command, you will get the output as shown below.
{ acknowledged: true, deletedCount: 1 }
{ _id: ObjectId("6371fd850f19826ee6ca5138"),
sl: 0,
Name: 'Alen'
}
请注意
The commands shown in this article are for MongoDB databases and need to be run on the MongoDB console.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
$unset operator in MongoDB
Publish Date:2025/04/27 Views:77 Category:MongoDB
-
This article will discuss how the $unset operator works in MongoDB. Additionally, we will demonstrate the use of this operator to remove a field from all documents in a MongoDB collection. $unset operator in MongoDB $unset is an operator us
Compass Filters in MongoDB
Publish Date:2025/04/27 Views:132 Category:MongoDB
-
This short article will cover the various ways to use Compass filters in MongoDB . Compass Filters in MongoDB MongoDB has a GUI called Compass . It is also known as MongoDB GUI. Users can use MongoDB to inspect the contents of their stored
Sorting by timestamp in MongoDB
Publish Date:2025/04/27 Views:54 Category:MongoDB
-
This article will introduce various methods of sorting timestamps in MongoDB. Sorting by timestamp in MongoDB sort() The method will sort documents in MongoDB. The method accepts a document containing a list of fields and the order in which
Deleting a user from a database in MongoDB
Publish Date:2025/04/27 Views:50 Category:MongoDB
-
This article will explain how to delete a user from a MongoDB database. In addition, we will see an example to make the topic easier to understand. Deleting a User from a MongoDB Database Sometimes we need to remove a particular user from t
$ne operator in MongoDB
Publish Date:2025/04/11 Views:84 Category:MongoDB
-
This article will discuss how the $ne operator works in MongoDB. In addition, we will list its differences from the $not operator. $ne operator in MongoDB $ne is an operator in MongoDB that stands for not equal to. This will compare the val
MongoDB $Set Operator
Publish Date:2025/04/11 Views:159 Category:MongoDB
-
With the help of this article, you will learn how to use $set the operator to partially update objects in MongoDB so that the new object overlaps/merges with the existing object. The $set operator replaces the value of a field with a given
Difference between $push and $addToSet in MongoDB
Publish Date:2025/04/11 Views:63 Category:MongoDB
-
This article explains the operators in MongoDB. What is the purpose of $push and $addToSet operators. Furthermore, the difference between these two operators is given in the code snippet. This article discusses the following topics. Operato
Sort a collection by date in MongoDB
Publish Date:2025/04/11 Views:64 Category:MongoDB
-
In this MongoDB tutorial, the problem of sorting a collection in MongoDB is discussed. The different ways to sort a collection in the database are briefly explained. Using sort() function in MongoDB This problem is solved using the MongoDB
Counting records in MongoDB
Publish Date:2025/04/11 Views:146 Category:MongoDB
-
This article discusses operators in MongoDB, aggregation operators, and different ways to calculate the total number of records. Operations in MongoDB CRUD operations are a user interface concept that allows users to browse, search, and cha