
- 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
How to pop a single value in MongoDB?
You can use pop() for this. Let us first create a collection with documents −
> db.persistChangeDemo.insertOne({"Name" : "Larry", "CreditScore": [500,700,760,100]}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfc52cbf3115999ed51203") }
Following is the query to display all documents from a collection with the help of find() method −
> db.persistChangeDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cdfc52cbf3115999ed51203"), "Name" : "Larry", "CreditScore" : [ 500, 700, 760, 100 ] }
Following is the query to pop a value −
> myDocument.CreditScore.pop(); 100
Let us save the above document −
> db.persistChangeDemo.save(myDocument); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the document once again −
> db.persistChangeDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cdfc52cbf3115999ed51203"), "Name" : "Larry", "CreditScore" : [ 500, 700, 760 ] }
- Related Articles
- Decrement only a single value in MongoDB?
- Increment only a single value in MongoDB document?
- Script to add a single value to array in MongoDB collection?
- How to stop MongoDB in a single command?
- How to select a single field in MongoDB?
- How to return only a single property “_id” in MongoDB?
- Make MongoDB replace single array value with string?
- How to update a single field in a capped collection in MongoDB?
- MongoDB findOneAndUpdate() to update a single document
- How to find only a single document satisfying the criteria in MongoDB?
- How to check multiple columns for a single value in MySQL?
- Remove only a single document in MongoDB
- Update only a single document in MongoDB
- How to remove all documents from a collection except a single document in MongoDB?
- How to convert a single character to its integer value in Python?

Advertisements