- 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
How do I remove a string from an array in a MongoDB document?
You can use $pull operator to remove a string from an array. Let us first create a collection with documents −
> db.removeAStringDemo.insertOne({"Score":[45,67,89,"John",98,99,67]}); { "acknowledged" : true, "insertedId" : ObjectId("5cda5224b50a6c6dd317adbd") }
Following is the query to display all documents from a collection with the help of find() method −
> db.removeAStringDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cda5224b50a6c6dd317adbd"), "Score" : [ 45, 67, 89, "John", 98, 99, 67 ] }
Following is the query to remove a string from an array in a MongoDB document. The string which is to be removed is “John” −
> db.removeAStringDemo.update( { _id :ObjectId("5cda5224b50a6c6dd317adbd") }, { $pull: { "Score":"John" }}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the document once again −
> db.removeAStringDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cda5224b50a6c6dd317adbd"), "Score" : [ 45, 67, 89, 98, 99, 67 ] }
- Related Articles
- How do I delete array value from a document in MongoDB?
- How to remove an element from a doubly-nested array in a MongoDB document?
- MongoDB query to remove array elements from a document?
- How do I remove a particular element from an array in JavaScript
- How to remove a field completely from a MongoDB document?
- How to query a document in MongoDB comparing fields from an array?
- Remove values from a matrix like document in MongoDB
- How to project specific fields from a document inside an array in Mongodb?
- How do I recursively remove consecutive duplicate elements from an array?
- How do I get email-id from a MongoDB document and display with print()
- How do I remove a substring from the end of a string in Python?
- How to remove all documents from a collection except a single document in MongoDB?
- Remove only a single document in MongoDB
- How can I remove a specific item from an array in JavaScript
- How can I remove a specific item from an array JavaScript?

Advertisements