db.cata.aggregate([{"$unwind":"$review"},{$group:{_id:"review",cnt:{$sum:1}}}]).pretty()
db.cata.aggregate([{"$unwind":"$review"},{$group:{_id:"$review",cnt:{$sum:1}}}]).pretty()
what these two queries do in document..how $review and review do...how $ works with fields...
my document is
{
"_id" : ObjectId("56dd01bdf3b660327b932da1"),
"product" : "super",
"price" : 10,
"review" : [
{
"user" : "fred",
"comment" : "great",
"rating" : 10
},
{
"user" : "tom",
"comment" : "i agree",
"rating" : 3
},
{
"user" : "vin",
"comment" : "good",
"rating" : 9
},
{
"user" : "anubhav",
"comment" : "too good",
"rating" : 8
}]
}
An aggregation pipeline consists of one or more stages that process documents: Each stage performs an operation on the input documents. For example, a stage can filter documents, group documents, and calculate values. The documents that are output from a stage are passed to the next stage.
Returns: A cursor to the documents produced by the final stage of the aggregation pipeline operation, or if you include the explain option, the document that provides details on the processing of the aggregation operation. If the pipeline includes the $out operator, aggregate() returns an empty cursor.
You can include one or more $addFields stages in an aggregation operation. To add field or fields to embedded documents (including documents in arrays) use the dot notation. See example. To add an element to an existing array field with $addFields , use with $concatArrays .
What is the Aggregation Pipeline in MongoDB? The aggregation pipeline refers to a specific flow of operations that processes, transforms, and returns results. In a pipeline, successive operations are informed by the previous result. In the above example, input refers to one or more documents.
Per the Mongodb doc said
The operand is a field path:
{ $unwind: <field path> }
To specify a field path, prefix the field name with a dollar sign
$
and enclose in quotes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With