0% found this document useful (0 votes)
10 views

Methods in MongoDB.pptx

The document explains various methods in MongoDB, including projection, limit, skip, sort, and GridFS for managing data retrieval and storage. It details how to use the find() method for selecting specific fields, limiting the number of documents, skipping records, and sorting results. Additionally, it covers GridFS for storing large files by dividing them into chunks and provides examples for each method discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Methods in MongoDB.pptx

The document explains various methods in MongoDB, including projection, limit, skip, sort, and GridFS for managing data retrieval and storage. It details how to use the find() method for selecting specific fields, limiting the number of documents, skipping records, and sorting results. Additionally, it covers GridFS for storing large files by dividing them into chunks and provides examples for each method discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Methods in MongoDB

Projection
• In MongoDB, projection means selecting only the necessary data rather
than selecting whole of the data of a document. If a document has 5 fields
and you need to show only 3, then select only 3 fields from them.
• MongoDB's find() method, explained in MongoDB Query Document accepts
second optional parameter that is list of fields that you want to retrieve.
• In MongoDB, when you execute find() method, then it displays all fields of a
document.
• To limit this, you need to set a list of fields with value 1 or 0.
• 1 is used to show the field while
• 0 is used to hide the fields.
Projection
• Syntax
• The basic syntax of find() method with projection is as follows −

• >db.COLLECTION_NAME.find({},{KEY:1})
• Example
• Consider the collection mycol has the following data −

• {_id : ObjectId("507f191e810c19729de860e1"), title: "MongoDB Overview"},


• {_id : ObjectId("507f191e810c19729de860e2"), title: "NoSQL Overview"},
• {_id : ObjectId("507f191e810c19729de860e3"), title: "Tutorials Point Overview"}
• Following example will display the title of the document while
querying the document.

• >db.mycol.find({},{"title":1,_id:0})
• {"title":"MongoDB Overview"}
• {"title":"NoSQL Overview"}
• {"title":"Tutorials Point Overview"}
•>
Limit
• 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.
• The basic syntax of limit() method is as follows −

• >db.COLLECTION_NAME.find().limit(NUMBER)
• Example
• Consider the collection myycol has the following data.

• {_id : ObjectId("507f191e810c19729de860e1"), title: "MongoDB Overview"},


• {_id : ObjectId("507f191e810c19729de860e2"), title: "NoSQL Overview"},
• {_id : ObjectId("507f191e810c19729de860e3"), title: "Tutorials Point Overview"}
SKIP
• Skip() method is used to skip the number of documents.
• >db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
• Example
• Following example will display only the second document.

• >db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1)
• {"title":"NoSQL Overview"}
•>
• Please note, the default value in skip() method is 0.
SORT
• To sort documents in MongoDB, you need to use sort() method. The method
accepts a document containing a list of fields along with their sorting order. To
specify sorting order 1 and -1 are used. 1 is used for ascending order while -1 is
used for descending order.
• Syntax
• The basic syntax of sort() method is as follows −

• >db.COLLECTION_NAME.find().sort({KEY:1})
• Example
• Consider the collection myycol has the following data.

• {_id : ObjectId("507f191e810c19729de860e1"), title: "MongoDB Overview"}


• Following example will display the documents sorted by title in the
descending order.

• >db.mycol.find({},{"title":1,_id:0}).sort({"title":-1})
• {"title":"Tutorials Point Overview"}
• {"title":"NoSQL Overview"}
• {"title":"MongoDB Overview"}
•>
• Please note, if you don't specify the sorting preference, then sort() method
will display the documents in ascending order.
GRIDFS
• Used for storing & retrieving large fies such as image,audio,video,etc.
• GridFS divides a file into chunks and stores each chunk of data in a
separate document, each of maximum size 255k.

• GridFS by default uses two collections fs.files and fs.chunks to store


the file's metadata and the chunks. Each chunk is identified by its
unique _id ObjectId field. The fs.files serves as a parent document.
The files_id field in the fs.chunks document links the chunk to its
parent.
• Following is a sample document of fs.files collection −

•{
• "filename": "test.txt",
• "chunkSize": NumberInt(261120),
• "uploadDate": ISODate("2014-04-13T11:32:33.557Z"),
• "md5": "7b762939321e146569b07f72c62cca4f",
• "length": NumberInt(646)
•}
• The document specifies the file name, chunk size, uploaded date, and length.
• Following is a sample document of fs.chunks document −

•{
• "files_id": ObjectId("534a75d19f54bfec8a2fe44b"),
• "n": NumberInt(0),
• "data": "Mongo Binary Data"
•}
• Adding Files to GridFS
• Now, we will store an mp3 file using GridFS using the put command. For this, we will
use the mongofiles.exe utility present in the bin folder of the MongoDB installation
folder.

• Open your command prompt, navigate to the mongofiles.exe in the bin folder of
MongoDB installation folder and type the following code −

• >mongofiles.exe -d gridfs put song.mp3


• Here, gridfs is the name of the database in which the file will be stored. If the
database is not present, MongoDB will automatically create a new document on the
fly. Song.mp3 is the name of the file uploaded.
• To see the file's document in database, you can use find query −

• >db.fs.files.find()
• The above command returned the following document −

•{
• _id: ObjectId('534a811bf8b4aa4d33fdf94d'),
• filename: "song.mp3",
• chunkSize: 261120,
• uploadDate: new Date(1397391643474), md5:
"e4f53379c909f7bed2e9d631e15c1c41",
• length: 10401959

You might also like