

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read a specific key-value pair from a MongoDB collection?
You can use dot(.) notation to read a specific key-value pair from MongoDB collection. Let us first create a collection with documents −
> db.readSpecificKeyValueDemo.insertOne( ... { ... "_id": 100, ... "StudentDetails": ... { ... "StudentFirstName" : "David", ... "StudentLastName" :"Miller", ... "StudentAge":23, ... "StudentCountryName":"US" ... } ... } ... ); { "acknowledged" : true, "insertedId" : 100 }
Following is the query to display all documents from the collection with the help of find() method −
> db.readSpecificKeyValueDemo.find().pretty();
This will produce the following output −
{ "_id" : 100, "StudentDetails" : { "StudentFirstName" : "David", "StudentLastName" : "Miller", "StudentAge" : 23, "StudentCountryName" : "US" } }
Following is the query to read a specific key-value pair from a MongoDB collection −
> db.readSpecificKeyValueDemo.find({},{"StudentDetails.StudentCountryName":1}).pretty();
This will produce the following output −
{ "_id" : 100, "StudentDetails" : { "StudentCountryName" : "US" } }
- Related Questions & Answers
- MongoDB query to update a specific document from a collection
- MongoDB query to find a specific city record from a collection
- How to sum the value of a key across all documents in a MongoDB collection?
- MongoDB query to pull a specific value from a document
- How to retrieve a value from MongoDB by its key name?
- Java Program to remove key value pair from HashMap?
- Add a key value pair to dictionary in Python
- How to drop a numeric collection from MongoDB?
- How to get array from a MongoDB collection?
- Find a value in lowercase from a MongoDB collection with documents
- Changing the primary key on a MongoDB collection?
- Project specific array field in a MongoDB collection?
- Retrieve data from a MongoDB collection?
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
Advertisements