MongoDB Projection Query Working-Programmershelper

0
210
Spread the love

Introduction

In this article, we learn MongoDB projection query working.Projection means you want to select specific field out of the whole document.In MongoDB <find()>mehtod. we do not specify which field return. it returns all the field in the document.0 or 1 is a projection parameter.So adding 1or 0 whether it should return fields or not.while 0 will use for hiding a field and 1 will use for showing fields.

Example: Suppose a collection has 10 document and every document has 7 fields and you want to return 7 out of 3 fields then you will use projection.

Prerequisite

It will good to cover my below articles before this.It helps to learn more about MongoDB.In which I explained.Click here MongoDB Indexing working and MongoDB Limit Document working and MongoDB find method working.

Implementation section

  • Projection query

Projection

The following is the basic syntax-

  • <db> is a reserved word.
  • <Collection_name> writes the collection name.
  • <KEY:1> for showing a field.
  • <KEY:0> for hiding a field.
  • Inside the bracket write the document <specific_criteria _for _Projection>.

Example:<db.Collection.find(){ “amount” : 1030},{“status”:1})>

Practical work

Consider we have the following data-

Above document, we have three document. we apply projection on it and it will return only <status>.So the following query display status field from a document.

Output:

Alt tag MongoDB projection

MongoDB projection, _id field always displays if you want to not show _id field. you can set_id field 0.then it will hide the _id field.

Conclusion

I wish I could tell you that a great site of MongoDB.you just understands key element above post-MongoDB projection working. more, detail about MongoDB projection, please click here projection working.You also read my previous lecture.I hope you will understand this lecture.Thank you for reading this lecture. Hope you got the idea. please share it.