Database Reference
In-Depth Information
],
"ok" : 1
}
You will notice that both queries now contain the same document, and they differ only in the count. This means that
our sort has been applied before the limit and allows us to get a consistent result. These operators should give you an
idea of the power you can drive by building a pipeline of operators to manipulate things until we get the desired result.
$unwind
The next operator we will look at is $unwind . This takes an array and splits each element into a new document
(in memory and not added to your collection) for each array element. As with making a shell pipeline, the best way to
understand what is output by the $unwind operator is simply to run it on its own and evaluate the output. Let's check
out the results of $unwind :
db.aggregation.aggregate({ $unwind : "$vegetables" });
{
"result" : [
{
"_id" : ObjectId("51de841747f3a410e3000001"),
"num" : 1,
"color" : "blue",
"transport" : "train",
"fruits" : [
"orange",
"banana",
"kiwi"
],
"vegetables" : "corn"
},
{
"_id" : ObjectId("51de841747f3a410e3000001"),
"num" : 1,
"color" : "blue",
"transport" : "train",
"fruits" : [
"orange",
"banana",
"kiwi"
],
"vegetables" : "brocoli"
},
{
"_id" : ObjectId("51de841747f3a410e3000001"),
"num" : 1,
"color" : "blue",
"transport" : "train",
"fruits" : [
"orange",
"banana",
"kiwi"
 
Search WWH ::




Custom Search