 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing an array element from MongoDB collection using update() and $pull
Let us first create a collection with documents -
> db.removingAnArrayElementDemo.insertOne({"UserMessage":["Hi","Hello","Bye"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef97bdef71edecf6a1f6a4") } Display all documents from a collection with the help of find() method -
> db.removingAnArrayElementDemo.find().pretty();
Output
{    "_id" : ObjectId("5cef97bdef71edecf6a1f6a4"),    "UserMessage" : [       "Hi",       "Hello",       "Bye"    ] } Following is the query to remove an array element from MongoDB -
> db.removingAnArrayElementDemo.update(    {_id:ObjectId("5cef97bdef71edecf6a1f6a4")},    { "$pull": { "UserMessage": "Hello" } } ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) Let us check the document once again:
> db.removingAnArrayElementDemo.find().pretty();
Output
{    .    "_id" : ObjectId("5cef97bdef71edecf6a1f6a4"),    "UserMessage" : [       "Hi",       "Bye"    ] }Advertisements
 