
- 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
Selecting only a single field from MongoDB?
You can use $and operator. Let us first create a collection with documents −
>db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"John","StudentAge":23,"StudentCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd547142cba06f46efe9f02") } >db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"Carol","StudentAge":21,"StudentCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5471f2cba06f46efe9f03") } >db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"David","StudentAge":24,"StudentCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd5472c2cba06f46efe9f04") } >db.selectingASingleFieldDemo.insertOne({"StudentFirstName":"Robert","StudentAge":26,"StudentCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd548382cba06f46efe9f05") }
Following is the query to display all documents from a collection with the help of find() method −
> db.selectingASingleFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd547142cba06f46efe9f02"), "StudentFirstName" : "John", "StudentAge" : 23, "StudentCountryName" : "US" } { "_id" : ObjectId("5cd5471f2cba06f46efe9f03"), "StudentFirstName" : "Carol", "StudentAge" : 21, "StudentCountryName" : "UK" } { "_id" : ObjectId("5cd5472c2cba06f46efe9f04"), "StudentFirstName" : "David", "StudentAge" : 24, "StudentCountryName" : "AUS" } { "_id" : ObjectId("5cd548382cba06f46efe9f05"), "StudentFirstName" : "Robert", "StudentAge" : 26, "StudentCountryName" : "AUS" }
Following is the query to select only a single field from MongoDB −
> db.selectingASingleFieldDemo.find( ... { $and:[{StudentAge: { $gt:21 } }, ... {"StudentCountryName" : "AUS"}] }, ... {StudentFirstName: 1, _id: 0});
This will produce the following output −
{ "StudentFirstName" : "David" } { "StudentFirstName" : "Robert" }
- Related Articles
- Display only a single field from all the documents in a MongoDB collection
- MongoDB query for a single field
- How can I sort documents in MongoDB 4 and display only a single field?
- Remove only a single document in MongoDB
- Update only a single document in MongoDB
- Decrement only a single value in MongoDB?
- MongoDB query select and display only a specific field from the document?
- How to select a single field in MongoDB?
- Increment only a single value in MongoDB document?
- Remove all except a single field from a nested document via projection in MongoDB
- Selecting a single row in MySQL?
- MongoDB query to update only a single item from voting (up and down) records?
- Retrieve only a single document specifying a criteria in MongoDB?
- How to return only a single property “_id” in MongoDB?
- Update only a single MongoDB document without deleting any date

Advertisements