
- 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 get items with a specific value from documents using MongoDB shell?
To get items with a specific value, simply use find(). Let us create a collection with documents −
> db.demo563.insertOne({"Name":"Chris","Age":21,"isMarried":true}){ "acknowledged" : true, "insertedId" : ObjectId("5e8f546c54b4472ed3e8e878") } > db.demo563.insertOne({"Name":"Bob","Age":23,"isMarried":false}){ "acknowledged" : true, "insertedId" : ObjectId("5e8f547854b4472ed3e8e879") } > db.demo563.insertOne({"Name":"Carol","Age":23,"isMarried":true}){ "acknowledged" : true, "insertedId" : ObjectId("5e8f548b54b4472ed3e8e87a") } > db.demo563.insertOne({"Name":"Mike","Age":21,"isMarried":true}){ "acknowledged" : true, "insertedId" : ObjectId("5e8f549454b4472ed3e8e87b") }
Display all documents from a collection with the help of find() method −
> db.demo563.find();
This will produce the following output −
{ "_id" : ObjectId("5e8f546c54b4472ed3e8e878"), "Name" : "Chris", "Age" : 21, "isMarried" : true } { "_id" : ObjectId("5e8f547854b4472ed3e8e879"), "Name" : "Bob", "Age" : 23, "isMarried" : false } { "_id" : ObjectId("5e8f548b54b4472ed3e8e87a"), "Name" : "Carol", "Age" : 23, "isMarried" : true } { "_id" : ObjectId("5e8f549454b4472ed3e8e87b"), "Name" : "Mike", "Age" : 21, "isMarried" : true }
Following is the query to get items from documents using MongoDB shell
> db.demo563.find({Age:21},{isMarried:true,Name:1,Age:1});
This will produce the following output −
{ "_id" : ObjectId("5e8f546c54b4472ed3e8e878"), "Name" : "Chris", "Age" : 21, "isMarried" : true } { "_id" : ObjectId("5e8f549454b4472ed3e8e87b"), "Name" : "Mike", "Age" : 21, "isMarried" : true }
- Related Articles
- MongoDB aggregation to fetch documents with specific field value?
- How to find MongoDB documents with a specific string?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Find value above a specific value in MongoDB documents?
- MongoDB query to get a specific number of items
- Get count of array elements from a specific field in MongoDB documents?
- MongoDB inverse of query to return all items except specific documents?
- MongoDB query to match documents with array values greater than a specific value
- How to calculate a sum of specific documents using MongoDB aggregation?
- MongoDB query to get specific list of names from documents where the value of a field is an array
- Find a value in lowercase from a MongoDB collection with documents
- Get MongoDB documents that contains specific attributes in array
- Match MongoDB documents with field value greater than a specific number and fetch them?
- Get a count of total documents with MongoDB while using limit?
- MongoDB query to get only specific fields in nested array documents?

Advertisements