Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
