- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Hide id field in MongoDB
Let us create a collection with documents −
> db.demo575.insertOne({id:101,Information:{Name:"Chris",Age:21}});{ "acknowledged" : true, "insertedId" : ObjectId("5e916a55581e9acd78b427f7") } > db.demo575.insertOne({id:102,Information:{Name:"David",Age:20}});{ "acknowledged" : true, "insertedId" : ObjectId("5e916a5f581e9acd78b427f8") } > db.demo575.insertOne({id:101,Information:{Name:"Bob",Age:23}});{ "acknowledged" : true, "insertedId" : ObjectId("5e916a67581e9acd78b427f9") }
Display all documents from a collection with the help of find() method −
> db.demo575.find();
This will produce the following output −
{ "_id" : ObjectId("5e916a55581e9acd78b427f7"), "id" : 101, "Information" : { "Name" : "Chris", "Age" : 21 } } { "_id" : ObjectId("5e916a5f581e9acd78b427f8"), "id" : 102, "Information" : { "Name" : "David", "Age" : 20 } } { "_id" : ObjectId("5e916a67581e9acd78b427f9"), "id" : 101, "Information" : { "Name" : "Bob", "Age" : 23 } }
Following is the query to hide id field values and display rest −
> db.demo575.find({id:101},{"Information.Name":1});
This will produce the following output −
{ "_id" : ObjectId("5e916a55581e9acd78b427f7"), "Information" : { "Name" : "Chris" } } { "_id" : ObjectId("5e916a67581e9acd78b427f9"), "Information" : { "Name" : "Bob" } }
Advertisements