WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MongoDB - Projection


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

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(124asd1sd4ae12), "title":"MongoDB"}
{ "_id" : ObjectId(124asd1sd4ae13), "title":"NoSQL "}
{ "_id" : ObjectId(124asd1sd4ae14), "title":"Welookups "}
>db.mycol.find({},{"title":1,_id:0})
{"title":"MongoDB "}
{"title":"NoSQL "}
{"title":"Welookups "}
>




Welookups is optimized for learning.Copy right 2018 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.