
- 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
Pull and add to set at the same time with MongoDB? Is it Possible?
Yes, you can use pull and add at the same time with $addToSet and $pull operator. Let us first create a collection with documents
> db.pullAndAddToSetDemo.insertOne({StudentScores : [78, 89, 90]} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9a797e15e86fd1496b38af") }
Following is the query to display all documents from a collection with the help of find() method
> db.pullAndAddToSetDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9a797e15e86fd1496b38af"), "StudentScores" : [ 78, 89, 90 ] }
Following is the query to pull and addtoset at the same time in MongoDB
> var addAndPull = db.pullAndAddToSetDemo.initializeOrderedBulkOp(); > addAndPull.find({ "StudentScores": 89 }).updateOne({ "$addToSet": { "StudentScores": 99 } }); > addAndPull.find({ "StudentScores": 90 }).updateOne({ "$pull": { "StudentScores": 90 } }); > addAndPull.execute();
This will produce the following output
BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 0, "nUpserted" : 0, "nMatched" : 2, "nModified" : 2, "nRemoved" : 0, "upserted" : [ ] })
Let us check the document once again from the collection. Following is the query
> db.pullAndAddToSetDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9a797e15e86fd1496b38af"), "StudentScores" : [ 78, 89, 99 ] }
- Related Articles
- MongoDB Indexes - Is it possible to create both normal & compound at the same time?
- Is it possible to add a set of elements in one cell with MySQL?
- Is it possible to have View and table with the same name in MySQL?
- MongoDB pull with positional operator?
- Is it possible to exclude nested fields in MongoDB with a wildcard?
- Is it possible to use MongoDB capped collection?
- MongoDB query to $pull / $unset with multiple conditions?
- Is it possible to cast in a MongoDB Query?
- Is it possible to add HTML5 validation to Visual Studio?
- Is it possible to achieve a slice chain in MongoDB?
- Is it possible to write to MongoDB console in JavaScript execution?
- Is it possible to utilize $addToSet multiple times in the same update?
- How to add a number to a current value in MySQL (multiple times at the same time)?
- Is it possible to rename _id field after MongoDB group aggregation?
- Is it possible to make a case-insensitive query in MongoDB?

Advertisements