
- 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
Return a specific field in MongoDB?
Too return a specific field, use the find() method in MongoDB. Let us first create a collection with documents −
> db.specificFieldDemo.insertOne({"FirstName":"John","LastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5cb8019a623186894665ae31") } > db.specificFieldDemo.insertOne({"FirstName":"John","LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cb801ab623186894665ae32") } > db.specificFieldDemo.insertOne({"FirstName":"David","LastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cb801b3623186894665ae33") } > db.specificFieldDemo.insertOne({"FirstName":"Sam","LastName":"Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5cb801bf623186894665ae34") }
Following is the query to display all documents from the collection with the help of find() method −
> db.specificFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cb8019a623186894665ae31"), "FirstName" : "John", "LastName" : "Doe" } { "_id" : ObjectId("5cb801ab623186894665ae32"), "FirstName" : "John", "LastName" : "Smith" } { "_id" : ObjectId("5cb801b3623186894665ae33"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5cb801bf623186894665ae34"), "FirstName" : "Sam", "LastName" : "Williams" }
Following is the query to return specific field. Here, we are returning the field “LastName” −
> db.specificFieldDemo.find({},{_id:0,LastName:1});
This will produce the following output −
{ "LastName" : "Doe" } { "LastName" : "Smith" } { "LastName" : "Miller" } { "LastName" : "Williams" }
- Related Articles
- Search by specific field in MongoDB
- Fetch specific field values in MongoDB
- Project specific array field in a MongoDB collection?
- Return specific MongoDB embedded document
- MongoDB function to return a specific data/value?
- Find all collections in MongoDB with specific field?
- Find documents that contains specific field in MongoDB?
- How to return only value of a field in MongoDB?
- Find MongoDB documents that contains specific field?
- MongoDB query (aggregation framework) to match a specific field value
- Find the document by field name with a specific value in MongoDB?
- Get count of array elements from a specific field in MongoDB documents?
- Update all the values of a field with a specific string in MongoDB?
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB query to add up the values of a specific field in documents

Advertisements