
- 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
MongoDB query to pull array element from a collection?
Use the $pull operator to pull array element from a collection. Let us first create a collection with documents −
> db.pullElementFromAnArrayDemo.insertOne( ... { ... "StudentScores":[89,56,78,90] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd0104a588d4a6447b2e063") }
Following is the query to display all documents from a collection with the help of find() method −
> db.pullElementFromAnArrayDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd0104a588d4a6447b2e063"), "StudentScores" : [ 89, 56, 78, 90 ] }
Following is the query to pull array element from a collection. Here, we are removing element 78 −
> db.pullElementFromAnArrayDemo.update({},{ $pull: { StudentScores: 78 } }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the document once again −
> db.pullElementFromAnArrayDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd0104a588d4a6447b2e063"), "StudentScores" : [ 89, 56, 90 ] }
- Related Articles
- Removing an array element from MongoDB collection using update() and $pull
- MongoDB query to pull multiple values from array
- MongoDB query to remove entire array from collection?
- Removing an array element from a MongoDB collection
- MongoDB query to pull a specific value from a document
- MongoDB query to remove entire data from a collection
- MongoDB query to update a specific document from a collection
- MongoDB query to match and remove element from an array?
- MongoDB query to remove element from array as sub property
- How to pull an array element (which is a document) in MongoDB?
- How to get array from a MongoDB collection?
- MongoDB query to rename a collection?
- MongoDB query to find a specific city record from a collection
- Pull multiple objects from an array in MongoDB?
- Pull an element in sub of sub-array in MongoDB?

Advertisements