Home >>MongoDB Tutorial >MongoDB Projection
The find() method of MongoDB, defined in the MongoDB Query Document, accepts the second optional parameter, which is a list of fields you want to retrieve. When you execute the find() method in MongoDB, all fields of a document are displayed. To limit this, a list of fields with a value of 1 or 0. must be set. 1 is used for showing a field, while 0 is used for hiding a field.
Syntax
The basic syntax with projection for the find() method 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: "phptpoint Over
The following example shows the title of the document when the document is being queried.
>db.mycol.find({},{"title":1,_id:0}) {"title":"MongoDB Overview"} {"title":"NoSQL Overview"} {"title":"phptpoint Overview"} >
Please note _id field is always displayed while executing find() method, if you don't want this field, then you need to set it as 0.