- 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
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" } ] }
- Related Articles
- MongoDB query to group records and display a specific value with dot notation
- Query a nested field within an array with MongoDB
- Dot notation vs Bracket notation in JavaScript
- What is python .. ("dot dot") notation syntax?
- Dot notation in JavaScript
- MongoDB query by sub-field?
- Count boolean field values within a single MySQL query?
- MongoDB query for a single field
- MySQL query to perform selection using AND OR
- MySQL query to search between comma separated values within one field?
- MongoDB Query for boolean field as “not true”
- How to query on list field in MongoDB?
- MongoDB query to update array with another field?
- Safely setting object properties with dot notation strings in JavaScript
- Update MongoDB field using value of another field?

Advertisements