Database Reference
In-Depth Information
],
"vegetables" : "potato"
},
...
],
"ok" : 1
}
We now have 3000 documents in our result array, a version of each document that has its own vegetable and
the rest of the original source document! You can see the power of what we can do with $unwind and how with a
very large collection of giant documents you could get yourself in trouble. Always remember that if you run your
match first, you can cut down the number of objects you want to work with before running the other, more intensive
aggregation operations.
$project
Our next operator, $project , is used to limit the fields or to rename fields returned as part of a document. This is just
like the field-limiting arguments that can be set on find commands. It's the perfect way to cut down on any excess
fields returned by your aggregations. Let's say we want to see only the fruit and vegetables for each of our documents;
we can provide a document that shows which elements we want to be displayed (or not) just as we would add to our
find command. Take the following example:
[
{ $unwind : "$vegetables" },
{ $project : { _id: 0, fruits:1, vegetables:1 } }
]
This projection returns the following result:
{
"result" : [
{
"fruits" : [
"orange",
"banana",
"kiwi"
],
"vegetables" : "corn"
},
{
"fruits" : [
"orange",
"banana",
"kiwi"
],
"vegetables" : "brocoli"
},
{
"fruits" : [
"orange",
"banana",
 
Search WWH ::




Custom Search