
- 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
MongoDB query select and display only a specific field from the document?
Let us first create a collection with documents −
> db.querySelectDemo.insertOne({UserId:100,UserName:"Chris",UserAge:25}); { "acknowledged" : true, "insertedId" : ObjectId("5ce90eb478f00858fb12e90e") } > db.querySelectDemo.insertOne({UserId:101,UserName:"Robert",UserAge:26}); { "acknowledged" : true, "insertedId" : ObjectId("5ce90ec578f00858fb12e90f") } > db.querySelectDemo.insertOne({UserId:103,UserName:"David",UserAge:27}); { "acknowledged" : true, "insertedId" : ObjectId("5ce90ed478f00858fb12e910") }
Following is the query to display all documents from a collection with the help of find() method −
> db.querySelectDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ce90eb478f00858fb12e90e"), "UserId" : 100, "UserName" : "Chris", "UserAge" : 25 } { "_id" : ObjectId("5ce90ec578f00858fb12e90f"), "UserId" : 101, "UserName" : "Robert", "UserAge" : 26 } { "_id" : ObjectId("5ce90ed478f00858fb12e910"), "UserId" : 103, "UserName" : "David", "UserAge" : 27 }
Following is the query to include only a specific field by setting it to TRUE in find() −
> db.querySelectDemo.find({},{_id:0,UserName:true});
This will produce the following output −
{ "UserName" : "Chris" } { "UserName" : "Robert" } { "UserName" : "David" }
- Related Articles
- How to select MongoDB document that does not consist a specific field?
- MongoDB query to update a specific document from a collection
- MongoDB query to pull a specific value from a document
- MongoDB query to remove a specific document
- How to run MongoDB query to update only a specific field value?
- Update only a specific value in a MongoDB document
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- How to select objects where an array contains only a specific field in MongoDB?
- MongoDB query to return only embedded document?
- Querying only the field name and display only the id in MongoDB?
- Display only a single field from all the documents in a MongoDB collection
- Filter specific values from a MongoDB document
- Find the document by field name with a specific value in MongoDB?
- MongoDB query to implement nor query to fetch documents except a specific document
- How to select specific columns in MongoDB query?

Advertisements