Mongodb Exp 2 New
Mongodb Exp 2 New
a) Develop a MongoDB query to select certain fields and ignore some fields of the document
from any collection
MongoDB Projection
MongoDB Projection is used when we want to get the selected fields of the documents rather
than all fields.
For example, we have a collection where we have stored documents that have the fields:
student_name, student_id, student_age but we want to see only the student_id of all the students
then in that case we can use projection to get only the student_id.
SYNTAX:
db.collection_name.find({},{field_key:1or0})
EXAMPLE:
Use the collection “studentdata”
>db.studentdata.find()
{
"_id":ObjectId("59bf63380be1"),
"student_name":"Adarsh",
"student_id" : 2002,
"student_age":22
},
{
"_id":ObjectId("59bf63500be1d"),
"student_name" : "Sidhu",
"student_id" : 2003,
"student_age":22
},
{
"_id":ObjectId("59bf63650be1d"),
"student_name" : "Mallikarjun",
"student_id" : 2004,
"student_age":23
}
Example1
To get only the student_id for all the documents, we will use the Projection like this:
db.studentdata.find({},{"_id":0,"student_id":1})
Output {"student_id":2002}
{"student_id":2003}
{"student_id":2004}
Example2
To get Student Name of all students, we wil luse the projection like this:
db.studentdata.find({},{_id:0,student_name:1}
OUTPUT {"student_name":Adarsh}
{"student_name":Sidhu}
{"student_name":Malikarjun}
Example3
To get Student Name and Student id of all the student we will use the projection like this:
db.studentdata.find({},{_id:0,student_id:1,student_name:1})
{"student_id":2003 “student_name”:Sidhu}
{"student_id":2004 “student_name”:Malikarjun}
Value 1 means show that field and 0 means do not show that field. When we set a field to 1 in
Projection other fields are automatically set to 0, except _id, so to avoid the _id we need to
specifically set it to 0inprojection. The vice versa is also true when we set few fields to 0, other
fields set to 1 automatically.
>db.studentdata.find({},{"_id":0,"student_name":0,"student_age":0})
OUTPUT {"student_id":2002}
{"student_id":2003}
{"student_id":2004}
b) Develop a MongoDB querry to display the first 5 documents from the result obtained in a collection
[use of limit and find]
{
_id:ObjectId("507f191e810c19729de860e1"),
title:"MongoD BOverview"
Author:“Rajkumar”
},
{
_id:ObjectId("507f191e810c19729de860e2"),
title: "NoSQL Overview"
Author:“Shivarajkumar”
},
{
_id:ObjectId("507f191e810c19729de860e3"),
title: "Tutorials PointOverview"
Author:“Punit”
}
Example1
Following example will display only two documents while querying the document.
>db.mycol.find({},{"title":1,_id:0}).limit(2)
{"title":"MongoDBOverview"}
{"title":"NoSQLOverview"}
Example2
Following example will display only two documents having field “Author” while querying the document.
db.mycol.find({},{Author:1,_id:0}).limit(2)
{"Author":"Rajkumar"}
{"Author":"Shivarajkumar"}
MongoDBSkip()Method
Apart from limit() method there is one more method skip() which also accepts number type argument and is used to skip
the number of documents.
Syntaxdb.Collection_Name.find().limit(number).skip(number)
{“title”:”NoSQLOverview”}
Example 4
The following example will display the document-4 to document-7 and skips the first 3 documents.
db.mycol.find({},{“title”:1_id:0}).limit(4).skip(3)
NoteIf you don't specify the number argument in limit() method then it will display all documents from the collection.