
- 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 for a single field
For a single field, use find(). Let us first create a collection with documents −
> db.demo10.insertOne({"StudentId":101,"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e0f68a7d7df943a7cec4f9b") } > db.demo10.insertOne({"StudentId":102,"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e0f68afd7df943a7cec4f9c") } > db.demo10.insertOne({"StudentId":103,"StudentName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e0f68b5d7df943a7cec4f9d") }
Following is the query to display all documents from a collection with the help of find() method −
> db.demo10.find();
This will produce the following output −
{ "_id" : ObjectId("5e0f68a7d7df943a7cec4f9b"), "StudentId" : 101, "StudentName" : "Chris" } { "_id" : ObjectId("5e0f68afd7df943a7cec4f9c"), "StudentId" : 102, "StudentName" : "David" } { "_id" : ObjectId("5e0f68b5d7df943a7cec4f9d"), "StudentId" : 103, "StudentName" : "Bob" }
Here is the query for a single field −
> db.demo10.find({},{"_id":0,"StudentId":0});
This will produce the following output −
{ "StudentName" : "Chris" } { "StudentName" : "David" } { "StudentName" : "Bob" }
- Related Articles
- MongoDB Query for boolean field as “not true”
- Selecting only a single field from MongoDB?
- MongoDB query by sub-field?
- How to select a single field in MongoDB?
- Count boolean field values within a single MySQL query?
- Update multiple rows in a single MongoDB query?
- Query a nested field within an array with MongoDB
- MongoDB query for counting number of unique fields grouped by another field?
- MongoDB query to search for string like “@email” in the field values
- Perform group and distinct together in a single MongoDB query
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB query to limit the returning values of a field?
- Query MongoDB for a nested search
- How to update a single field in a capped collection in MongoDB?
- How to query on list field in MongoDB?

Advertisements