
- 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 pull with positional operator?
Use $pull operator along with positional operator($) in MongoDB. Let us first create a collection with documents −
> db.pullWithPositionalOperatorDemo.insertOne( ... { ... _id: 100, ... "StudentDetails": [ ... { ... "StudentId": "STU-1", ... "StudentFavouriteSubject": ["MongoDB", "Java"] ... }, ... { ... "StudentId": "STU-2", ... "StudentFavouriteSubject": ["PHP", "MySQL"] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 100 }
Following is the query to display all documents from a collection with the help of find() method −
> db.pullWithPositionalOperatorDemo.find().pretty();
This will produce the following output −
{ "_id" : 100, "StudentDetails" : [ { "StudentId" : "STU-1", "StudentFavouriteSubject" : [ "MongoDB", "Java" ] }, { "StudentId" : "STU-2", "StudentFavouriteSubject" : [ "PHP", "MySQL" ] } ] }
Following is the query to perform pull with positional operator −
> db.pullWithPositionalOperatorDemo.update({ ... "StudentDetails" : { ... "$elemMatch" : { ... "StudentId" : "STU-2", ... "StudentFavouriteSubject" : "MySQL" ... } ... } ... }, { ... $pull : { ... "StudentDetails.$.StudentFavouriteSubject" : "MySQL" ... } ... }, { ... multi : true ... }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check all documents from the above collection once again −
> db.pullWithPositionalOperatorDemo.find().pretty();
This will produce the following output −
{ "_id" : 100, "StudentDetails" : [ { "StudentId" : "STU-1", "StudentFavouriteSubject" : [ "MongoDB", "Java" ] }, { "StudentId" : "STU-2", "StudentFavouriteSubject" : [ "PHP" ] } ] }
- Related Articles
- Using positional operator with hierarchies in MongoDB?
- Update a specific MongoDB document in array with $set and positional $ operator?
- MongoDB query to $pull / $unset with multiple conditions?
- Select multiple values with MongoDB OR operator
- MongoDB to fetch documents with $or Operator
- How to query with nand operator in MongoDB?
- Pull multiple objects from an array in MongoDB?
- MongoDB query to pull multiple values from array
- Positional Number System
- Pull and add to set at the same time with MongoDB? Is it Possible?
- MongoDB divide aggregation operator?
- MongoDB query to pull array element from a collection?
- Using $addFields pipeline and run with the MongoDB $filter operator
- Python - Remove positional rows
- How to pull value from array of ObjectIDs in MongoDB?

Advertisements