 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Field selection within MongoDB query using dot notation?
For this, use dot notation for field selection in MongoDB find(). Let us create a collection with documents −
> db.demo302.insertOne({"Id":101,"details":[{"Name":"Chris",Age:21,"Subject":"MySQL"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d746f5d93261e4bc9ea52") } > db.demo302.insertOne({"Id":102,"details":[{"Name":"Bob",Age:23,"Subject":"MongoDB"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d74815d93261e4bc9ea53") } > db.demo302.insertOne({"Id":103,"details":[{"Name":"David",Age:20,"Subject":"Java"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d74955d93261e4bc9ea54") } Display all documents from a collection with the help of find() method −
> db.demo302.find();
This will produce the following output −
{ "_id" : ObjectId("5e4d746f5d93261e4bc9ea52"), "Id" : 101, "details" : [ { "Name" : "Chris", "Age" : 21, "Subject" : "MySQL" } ] } { "_id" : ObjectId("5e4d74815d93261e4bc9ea53"), "Id" : 102, "details" : [ { "Name" : "Bob", "Age" : 23, "Subject" : "MongoDB" } ] } { "_id" : ObjectId("5e4d74955d93261e4bc9ea54"), "Id" : 103, "details" : [ { "Name" : "David", "Age" : 20, "Subject" : "Java" } ] } Following is the query for field selection using dot notation −
>db.demo302.find({"details.Subject":"MongoDB"},{"details.Name":0,"details.Age":0,_id:0,Id:0}); This will produce the following output −
{ "details" : [ { "Subject" : "MongoDB" } ] }Advertisements
 