MongoDB projection means to select only the necessary data instead of selecting the entire data for a file. If a document has 5 fields, you need to display only 3, and then select only 3 fields.
Find () method
MongoDB's Find () method, which is interpreted in the MongoDB query document, accepts the second optional parameter that is the list of fields to retrieve. In MongoDB, when the Find () method is executed, it displays all fields of a document. To limit this, you need to set the field List value 1 or 0. 1 is used to display the field and 0 is used to hide the field.
Grammar:
The Find () method has a projection basic syntax as follows
>db. Collection_name.find ({},{key:1})
Example
Consider the collection Myycol with the following data
{"_id": ObjectId (5983548781331ADF45EC5), "title": "MongoDB Overview""_id": ObjectId (5983548781331ADF45EC6) , "title": "NoSQL Overview""_id": ObjectId (5983548781331ADF45EC7), "title": "Yiibai Overview"}
The following example will show the title of the file and several questions about the file.
>db.mycol.find ({},{"title": 1,_id:0}) {"title": "MongoDB Overview"} {"title": "NoSQL Overview" } {"title": "Yiibai Overview"}>
Note that the _id field is always displayed in the Execute Find () method, and if you do not want this field, you need to set it to 0
MongoDB (ix) MongoDB projection