Methods in MongoDB.pptx
Methods in MongoDB.pptx
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 −
• >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.
• >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.
• >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.
•{
• "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 −
• >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