
- 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 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 Articles
- 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?
- How to retrieve a value from MongoDB by its key name?
- MongoDB query to pull a specific value from a document
- How to drop a numeric collection from MongoDB?
- How to get array from a MongoDB collection?
- How to get the child of a MongoDB collection by the key?
- Changing the primary key on a MongoDB collection?
- Find a value in lowercase from a MongoDB collection with documents
- Golang program to delete the item from the hash collection based on a specific key
- How to get items with a specific value from documents using MongoDB shell?
- Project specific array field in a MongoDB collection?
- Java Program to remove key value pair from HashMap?
- How to delete documents from a collection in MongoDB?

Advertisements