Friday, July 18, 2014

Projection

Project Array Values
Source - http://docs.mongodb.org/manual

$(projection) operator limits the contents of the field that is included in the query results to contain the first matching element.

Data
{ "_id" : 1, "semester" : 1, "grades" : [ 70, 87, 90 ] },
{ "_id" : 2, "semester" : 1, "grades" : [ 90, 88, 92 ] },
{ "_id" : 3, "semester" : 1, "grades" : [ 85, 100, 90 ] },
{ "_id" : 4, "semester" : 2, "grades" : [ 79, 85, 80 ] },
{ "_id" : 5, "semester" : 2, "grades" : [ 88, 88, 92 ] },
{ "_id" : 6, "semester" : 2, "grades" : [ 95, 90, 96 ] }

Criteria - returns only the first element greater than or equal to 85 for the grades field.
db.students.find( { semester: 1, grades: { $gte: 85 } },
                          { "grades.$": 1 } )
 or
db.students.find( { semester: 1, grades: { $gte: 85 } },
                   { "grades.$": -1 } )

Result
{ "_id" : 1, "grades" : [ 87 ] }
{ "_id" : 2, "grades" : [ 90 ] }
{ "_id" : 3, "grades" : [ 85 ] }

Limitations
1. limits the content of the field to the first element that matches the query document.
2. The field must appear in the query document
3. Only one positional $ operator can appear in the projection document.
4. Only one array field can appear in the query document.

Project Array Documents
Data
{ "_id" : 7, semester: 3, "grades" : [ { grade: 80, mean: 75, std: 8 },
                                       { grade: 85, mean: 90, std: 5 },
                                       { grade: 90, mean: 85, std: 3 } ] },
{ "_id" : 8, semester: 3, "grades" : [ { grade: 92, mean: 88, std: 8 },
                                       { grade: 78, mean: 90, std: 5 },
                                       { grade: 88, mean: 85, std: 3 } ] }

Criteria
db.test.find(
   { "grades.mean": { $gt: 70 } },
   { "grades.$": 1 }
)

Result
{ "_id" : 7, "grades" : [ { "grade" : 80, "mean" : 75, "std" : 8 } ] }
{ "_id" : 8, "grades" : [ { "grade" : 92, "mean" : 88, "std" : 8 } ] }


$elemMatch (projection)
limits the contents of an array field that is included in the query results to contain only the array element that matches the $elemMatch condition.

Data
{
 _id: 1,
 zipcode: "63109",
 students: [
              { name: "john", school: 102, age: 10 },
              { name: "jess", school: 102, age: 11 },
              { name: "jeff", school: 108, age: 15 }
           ]
},
{
 _id: 2,
 zipcode: "63110",
 students: [
              { name: "ajax", school: 100, age: 7 },
              { name: "achilles", school: 100, age: 8 },
           ]
},
{
 _id: 3,
 zipcode: "63109",
 students: [
              { name: "ajax", school: 100, age: 7 },
              { name: "achilles", school: 100, age: 8 },
           ]
},
{
 _id: 4,
 zipcode: "63109",
 students: [
              { name: "barney", school: 102, age: 7 },
           ]
}

Criteria
db.test.find( { zipcode: "63109" },
                 { students: { $elemMatch: { school: 102 } } } )

Result
{ "_id" : 1, "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] }
{ "_id" : 3 }
{ "_id" : 4, "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }

db.test.find( { zipcode: "63109" },
                 { students: { $elemMatch: { school: 102 } }, _id : 0 } )
Result
{ "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] }
{  }
{ "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }

Note : The document with _id equal to 3 does not contain the students field in the result since no element in its students array matched the $elemMatch condition.

Limitations
If multiple elements match the $elemMatch condition, the operator returns the first matching element in the array.

$slice(projection)

Returns first element
db.test.find( { zipcode : "63109" }, { students: {$slice: 1 } } )

Result
{ "_id" : 1, "zipcode" : "63109", "students" : [ { "name" : "john", "school" : 102, "age" : 10 } ] }
{ "_id" : 3, "zipcode" : "63109", "students" : [ { "name" : "ajax", "school" : 100, "age" : 7 } ] }
{ "_id" : 4, "zipcode" : "63109", "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }

Returns last 2 elements
db.test.find( { zipcode : "63109" }, { students: {$slice: -2 } } )

Result
{ "_id" : 1, "zipcode" : "63109", "students" : [ { "name" : "jess", "school" : 102, "age" : 11 }, { "name" : "jeff", "school" : 108, "age" : 15 } ] }
{ "_id" : 3, "zipcode" : "63109", "students" : [ { "name" : "ajax", "school" : 100, "age" : 7 }, { "name" : "achilles", "school" : 100, "age" : 8 } ] }
{ "_id" : 4, "zipcode" : "63109", "students" : [ { "name" : "barney", "school" : 102, "age" : 7 } ] }

No comments:

Post a Comment