- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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" ] }
- Related Articles
- Removing an array element from a MongoDB collection
- MongoDB query to pull array element from a collection?
- Pull multiple objects from an array in MongoDB?
- Pull an element in sub of sub-array in MongoDB?
- Update an array element matching a condition using $push in MongoDB
- Removing an element from an Array in Javascript
- MongoDB query to update an array element matching a condition using $push?
- Update Array element in MongoDB?
- How to pull even numbers from an array in MongoDB?
- How to update MongoDB collection using $toLower?
- How to pull an array element (which is a document) in MongoDB?
- How to update an existing document in MongoDB collection using Java?
- Removing an object in a child collection in MongoDB?
- MongoDB query to update an array using FindAndUpdate()?
- Searching for an array entry via its id in a MongoDB collection and performing update

Advertisements