
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Remove values from a matrix like document in MongoDB
To remove values from a matrix, use $pull in MongoDB. Let us create a collection with documents −
> db.demo632.insertOne( ... { ... "arrayMatrix": [ ... [10,20], ... [10,20], ... [10,20], ... [10,20], ... [10,20], ... [10,20] ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e9b27b46c954c74be91e6c0") }
Display all documents from a collection with the help of find() method −
> db.demo632.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e9b27b46c954c74be91e6c0"), "arrayMatrix" : [ [ 10, 20 ], [ 10, 20 ], [ 10, 20 ], [ 10, 20 ], [ 10, 20 ], [ 10, 20 ] ] }
Following is the query to remove values from a matrix like document in MongoDB −
> db.demo632.update( ... {}, ... { ... "$pull": { ... "arrayMatrix.0": 20, ... "arrayMatrix.1": 20, ... "arrayMatrix.2": 20, ... "arrayMatrix.3": 20, ... "arrayMatrix.4": 20, ... "arrayMatrix.5": 20 ... } ... } ... ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo632.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e9b27b46c954c74be91e6c0"), "arrayMatrix" : [ [ 10 ], [ 10 ], [ 10 ], [ 10 ], [ 10 ], [ 10 ] ] }
- Related Articles
- Filter specific values from a MongoDB document
- MongoDB query to remove subdocument from document?
- MongoDB query to remove array elements from a document?
- How to remove a field completely from a MongoDB document?
- Remove only a single document in MongoDB
- Remove only one document in MongoDB?
- How do I remove a string from an array in a MongoDB document?
- MongoDB query to remove a specific document
- How to subtract values (TotalPrice – Discount) from document field values in MongoDB?
- Remove document whose value is matched with $eq from a MongoDB collection?
- How to remove an element from a doubly-nested array in a MongoDB document?
- How to remove all documents from a collection except a single document in MongoDB?
- Remove all except a single field from a nested document via projection in MongoDB
- Accessing array values in a MongoDB collection to fetch a specific document
- How to remove document on the basis of _id in MongoDB?

Advertisements