
- 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 select a single field in MongoDB?
You can select a single field in MongoDB using the following syntax:
db.yourCollectionName.find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});
In the above syntax "yourSingleFieldName":1, _id:0 means get all data from one field without _id.
To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:
> db.singleFieldDemo.insertOne({"StudentName":"David","StudentAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5c6eba356fd07954a489067c") } > db.singleFieldDemo.insertOne({"StudentName":"Bob","StudentAge":18}); { "acknowledged" : true, "insertedId" : ObjectId("5c6eba406fd07954a489067d") } > db.singleFieldDemo.insertOne({"StudentName":"Chris","StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c6eba4c6fd07954a489067e") } > db.singleFieldDemo.insertOne({"StudentName":"Robert","StudentAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c6eba586fd07954a489067f") }
Now you can display all documents from a collection with the help of find() method. The query is as follows:
> db.singleFieldDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6eba356fd07954a489067c"), "StudentName" : "David", "StudentAge" : 28 } { "_id" : ObjectId("5c6eba406fd07954a489067d"), "StudentName" : "Bob", "StudentAge" : 18 } { "_id" : ObjectId("5c6eba4c6fd07954a489067e"), "StudentName" : "Chris", "StudentAge" : 24 } { "_id" : ObjectId("5c6eba586fd07954a489067f"), "StudentName" : "Robert", "StudentAge" : 26 }
Here is the query to select a single field:
> db.singleFieldDemo.find({"StudentAge":18},{"StudentName":1,"_id":0});
The following is the output:
{ "StudentName" : "Bob" }
- Related Articles
- How to update a single field in a capped collection in MongoDB?
- MongoDB query for a single field
- Selecting only a single field from MongoDB?
- How to select MongoDB document that does not consist a specific field?
- Select documents grouped by field in MongoDB?
- How to select objects where an array contains only a specific field in MongoDB?
- Unable to implement $addToSet in MongoDB to fetch values of a single field?
- MongoDB equivalent of SELECT field AS `anothername`?
- How can I sort documents in MongoDB 4 and display only a single field?
- How to increment a field in MongoDB?
- Display only a single field from all the documents in a MongoDB collection
- How to select a field corresponding to the field in which MAX() exists?
- How to stop MongoDB in a single command?
- How to pop a single value in MongoDB?
- How to make a unique field in MongoDB?

Advertisements