Mongodb Projection Document

By Yashwant Chavan, Views 1681, Date 06-Oct-2016

When you query for mongoDB collection, mongoDB by default return all fields in matching documents. To limit the data you can use projection which will help to include or exclude the document fields in the query output.

tags mongodb

Projection Document

 db.collection.find(query, projection);

Projection Document Syntax

Projection document limits the document fields for all matching documents. You can specify the inclusion or exclusion of field using true or false value. Refer below syntax

 { field1: <value>, field2: <value> ... }

<value> can be 1 or true to include the field

<value> can be 0 or false to exclude the field

Return All Fields in Matching Documents

You don't need to specify any projection criteria to get all fields of documents that match the query. In below example, We have not specified any projection to retrieve from the students collection db.students.find() . It includes all the documents fields.

> db.students.find().pretty()
{
        "_id" : ObjectId("57f86b4d70a327570eaff1d6"),
        "roll_no" : 1,
        "first_name" : "Yashwant",
        "last_name" : "Chavan",
        "class" : "X",
        "age" : 16,
        "address" : {
                "address" : "Near Mumbai - Pune Highway",
                "city" : "Pune",
                "locality" : "Wakad",
                "pin_code" : 411057
        },
        "subjects" : [
                {
                        "subject" : "English",
                        "score" : 80
                },
                {
                        "subject" : "Physics",
                        "score" : 60
                }
        ]
}
{
        "_id" : ObjectId("57f86b5570a327570eaff1d7"),
        "roll_no" : 2,
        "first_name" : "Dinesh",
        "last_name" : "Patil",
        "class" : "X",
        "age" : 15,
        "address" : {
                "address" : "Near Datta Mandir",
                "city" : "Pune",
                "locality" : "Wakad",
                "pin_code" : 411057
        },
        "subjects" : [
                {
                        "subject" : "English",
                        "score" : 50
                },
                {
                        "subject" : "Physics",
                        "score" : 55
                }
        ]
}
{
        "_id" : ObjectId("57f86b5a70a327570eaff1d8"),
        "roll_no" : 3,
        "first_name" : "Ganesh",
        "last_name" : "Patil",
        "class" : "X",
        "age" : 16,
        "address" : {
                "address" : "Near Bhumkar Chowk",
                "city" : "Pune",
                "locality" : "Wakad",
                "pin_code" : 411057
        },
        "subjects" : [
                {
                        "subject" : "English",
                        "score" : 77
                },
                {
                        "subject" : "Physics",
                        "score" : 56
                }
        ]
}

Return the Specified Fields

To include specific field of document use 1 or true in projection criteria. In this example only first_name and last_name field ({ first_name: 1, last_name: 1 }) is included by default, the _id fields return in the matching documents. You can remove the _id field from the results by specifying its exclusion { _id: 0 }

> db.students.find( { class: "X" }, { first_name: 1, last_name: 1 } ).pretty()
{
        "_id" : ObjectId("57f86b4d70a327570eaff1d6"),
        "first_name" : "Yashwant",
        "last_name" : "Chavan"
}
{
        "_id" : ObjectId("57f86b5570a327570eaff1d7"),
        "first_name" : "Dinesh",
        "last_name" : "Patil"
}
{
        "_id" : ObjectId("57f86b5a70a327570eaff1d8"),
        "first_name" : "Ganesh",
        "last_name" : "Patil"
}
>

Excluded Specific Field(s)

In below example, the age and address fields are excluded from documents { age: 0, address: 0 } .

> db.students.find( { class: "X" }, { age: 0, address: 0 } ).pretty()
{
        "_id" : ObjectId("57f86b4d70a327570eaff1d6"),
        "roll_no" : 1,
        "first_name" : "Yashwant",
        "last_name" : "Chavan",
        "class" : "X",
        "subjects" : [
                {
                        "subject" : "English",
                        "score" : 80
                },
                {
                        "subject" : "Physics",
                        "score" : 60
                }
        ]
}
{
        "_id" : ObjectId("57f86b5570a327570eaff1d7"),
        "roll_no" : 2,
        "first_name" : "Dinesh",
        "last_name" : "Patil",
        "class" : "X",
        "subjects" : [
                {
                        "subject" : "English",
                        "score" : 50
                },
                {
                        "subject" : "Physics",
                        "score" : 55
                }
        ]
}
{
        "_id" : ObjectId("57f86b5a70a327570eaff1d8"),
        "roll_no" : 3,
        "first_name" : "Ganesh",
        "last_name" : "Patil",
        "class" : "X",
        "subjects" : [
                {
                        "subject" : "English",
                        "score" : 77
                },
                {
                        "subject" : "Physics",
                        "score" : 56
                }
        ]
}
>

Return Specific Fields in Embedded Documents

You can use the dot notation to return specific fields in an embedded document. We have included city field from embedded document address e.g. {"address.city": 1}.If you want to suppress specific fields in embedded documents use 0 instead of 1.

> db.students.find({ class: "X" },{ roll_no: 1, first_name: 1, last_name: 1, "address.city": 1}).pretty()
{
        "_id" : ObjectId("57f86b4d70a327570eaff1d6"),
        "roll_no" : 1,
        "first_name" : "Yashwant",
        "last_name" : "Chavan",
        "address" : {
                "city" : "Pune"
        }
}
{
        "_id" : ObjectId("57f86b5570a327570eaff1d7"),
        "roll_no" : 2,
        "first_name" : "Dinesh",
        "last_name" : "Patil",
        "address" : {
                "city" : "Pune"
        }
}
{
        "_id" : ObjectId("57f86b5a70a327570eaff1d8"),
        "roll_no" : 3,
        "first_name" : "Ganesh",
        "last_name" : "Patil",
        "address" : {
                "city" : "Pune"
        }
}
>

Projection on Embedded Documents in an Array

In below example we have included subject and score fields in the documents from subjects array ({"subjects.subject": 1, "subjects.score": 1}).

> db.students.find( { class: "X" }, { first_name: 1, last_name: 1, "subjects.subject": 1, "subjects.score": 1
} ).pretty()
{
        "_id" : ObjectId("57f86b4d70a327570eaff1d6"),
        "first_name" : "Yashwant",
        "last_name" : "Chavan",
        "subjects" : [
                {
                        "subject" : "English",
                        "score" : 80
                },
                {
                        "subject" : "Physics",
                        "score" : 60
                }
        ]
}
{
        "_id" : ObjectId("57f86b5570a327570eaff1d7"),
        "first_name" : "Dinesh",
        "last_name" : "Patil",
        "subjects" : [
                {
                        "subject" : "English",
                        "score" : 50
                },
                {
                        "subject" : "Physics",
                        "score" : 55
                }
        ]
}
{
        "_id" : ObjectId("57f86b5a70a327570eaff1d8"),
        "first_name" : "Ganesh",
        "last_name" : "Patil",
        "subjects" : [
                {
                        "subject" : "English",
                        "score" : 77
                },
                {
                        "subject" : "Physics",
                        "score" : 56
                }
        ]
}
>
Yashwant Chavan

Yashwant Chavan

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan@gmail.com