- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB query to pull a specific value from a document
To pull a specific value, use UPDATE with $pull. Let us create a collection with documents −
> db.demo318.insertOne({Subject:["MySQL","MongoDB","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ea6df8647eb59e562062") } > db.demo318.insertOne({Subject:["Spring","Hibernate"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ea78f8647eb59e562063") }
Display all documents from a collection with the help of find() method −
> db.demo318.find();
This will produce the following output −
{ "_id" : ObjectId("5e50ea6df8647eb59e562062"), "Subject" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e50ea78f8647eb59e562063"), "Subject" : [ "Spring", "Hibernate" ] }
Following is the query to $pull a specific value “Hibernate” −
> db.demo318.update({Subject:"Hibernate"},{$pull:{"Subject":"Hibernate"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo318.find();
This will produce the following output −
{ "_id" : ObjectId("5e50ea6df8647eb59e562062"), "Subject" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e50ea78f8647eb59e562063"), "Subject" : [ "Spring" ] }
Advertisements