- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Querying only the field name and display only the id in MongoDB?
To query only the field name, set fieldName to 0 i.e. the fieldName to hide. Let us create a collection with documents −
> db.demo650.insertOne({_id:101,details:{Name:"Chris",Age:21}}); { "acknowledged" : true, "insertedId" : 101 } > db.demo650.insertOne({_id:102,details:{Name:"Bob",Age:22}}); { "acknowledged" : true, "insertedId" : 102 } > db.demo650.insertOne({_id:103,details:{Name:"Sam",Age:20}}); { "acknowledged" : true, "insertedId" : 103 } > db.demo650.insertOne({_id:104,details:{Name:"Robert",Age:24}}); { "acknowledged" : true, "insertedId" : 104 }
Display all documents from a collection with the help of find() method −
> db.demo650.find();
This will produce the following output −
{ "_id" : 101, "details" : { "Name" : "Chris", "Age" : 21 } } { "_id" : 102, "details" : { "Name" : "Bob", "Age" : 22 } } { "_id" : 103, "details" : { "Name" : "Sam", "Age" : 20 } } { "_id" : 104, "details" : { "Name" : "Robert", "Age" : 24 } }
Following is how to query only the field name in MongoDB. We wanted to hide details field, therefore we have set it to 0 −
> db.demo650.find({},{details:0});
This will produce the following output −
{ "_id" : 101 } { "_id" : 102 } { "_id" : 103 } { "_id" : 104 }
- Related Articles
- MongoDB query select and display only a specific field from the document?
- How to project grouping into object in MongoDB and display only the marks field?
- MongoDB query to fetch only the “Name” field based on roles?
- Display only a single field from all the documents in a MongoDB collection
- How can I sort documents in MongoDB 4 and display only a single field?
- Only display row with highest ID in MySQL
- Selecting only a single field from MongoDB?
- How to display only the keys from nested MongoDB documents?
- How to improve querying field in MongoDB?
- How can I display only unique record from MongoDB and ignore the duplicates?
- Hide id field in MongoDB
- How to return only value of a field in MongoDB?
- Display only an element found in an array in MongoDB?
- Update a table in MySQL and display only the initials name in a new column
- Update only the int in MySQL Field

Advertisements