0% found this document useful (0 votes)
23 views3 pages

Mongodb Exp 2 New

The document explains how to use MongoDB projection to select specific fields from a collection while ignoring others, providing syntax and examples for retrieving student data. It also covers the use of the limit() method to restrict the number of documents returned in a query and the skip() method to bypass a specified number of documents. Several examples illustrate these concepts using a 'studentdata' collection and a 'mycol' collection.

Uploaded by

mahesh kanjikar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Mongodb Exp 2 New

The document explains how to use MongoDB projection to select specific fields from a collection while ignoring others, providing syntax and examples for retrieving student data. It also covers the use of the limit() method to restrict the number of documents returned in a query and the skip() method to bypass a specified number of documents. Several examples illustrate these concepts using a 'studentdata' collection and a 'mycol' collection.

Uploaded by

mahesh kanjikar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Experiment–2

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
}

Example1

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}
Example2
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}
Example3
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})

OUTPUT  {"student_id":2002 “student_name”:Adarsh}

{"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.

Another way of doing the same thing:

>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]

The limit() Method


To limit the records in MongoDB, you need to use limit() method.The method accepts one number type argument,
which is the number of documents that you want to be displayed.

SyntaxThe basic syntax of limit() method is as follows−


>db.COLLECTION_NAME.find( ).limit(NUMBER)
Example

Consider the collection “mycol” has the following data.

{
_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"}

Example2
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.

Syntaxdb.Collection_Name.find().limit(number).skip(number)

Example3 The following example will display these


document. db.mycol.find({},
{“title”:1,_id:0}).limit(1).skip(1)

{“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)

NoteIf you don't specify the number argument in limit() method then it will display all documents from the collection.

You might also like