MongoDB - SPLessons

MongoDB Projection

Chapter 23

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

MongoDB Projection

MongoDB Projection

shape Description

MongoDB Projection can be described as a function in which only required information has to be retrieved from the document rather then retrieving the whole document.

Find()

shape Description

Find() function in MongoDB Projection has been utilized as a part of the Query records of MongoDB in which it takes the second parameter as  discretionary in which rundown of fields that need to be retrieved. While executing the find() function in MongoDB Projection, then it retrieve these find() will each an every fields of an archive.

If an array field contains multiple documents with the same field name and the find() method in MongoDB includes a sort() on that repeating field, the returned documents may not reflect the sort order because the sort was applied to the elements of the array before the $ projection operator.

shape Syntax

The syntax for find() method in MongoDB Projection is as follows:
>db.collection_name.find({},{KEY:1})

Collection_name => Is a document name

Find() – Will retrieve the required fields from the documents

shape Examples

By viewing the below example, the concept of find() method in MongoDB database can be easily understand.

MongoDB shell version:2.6.1
connecting to: test
>use mycol
switched to db mycol
>show collections
names
system.indexes
>db.mycol.find();
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Mongodb database"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Mongodb"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Mongod"}
>db.mycol.find{},{"title":1,_id:0})
{"title":"Mongodb database"}
{"title":"Mongodb"}
{"title":"Mongod"}

Here in the above example instead of retrieving the entire document, the find() method in MongoDB database will retrieve only the exact notation based on this method.

Summary

shape Key Points

  • MongoDB Projection – In which only required information will be retrived from the document instead of entire document.
  • Find() method – Is utilized as a part of the query record in the mongoDB.