Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does $ do with fields when aggregating in mongodb

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
    }]
}
like image 546
Laxman Singh Garia Avatar asked Mar 09 '16 07:03

Laxman Singh Garia


People also ask

How does aggregate work in MongoDB?

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.

What does MongoDB aggregation return?

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.

How do you add a field in aggregate?

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 purpose of the aggregation pipeline in MongoDB?

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.


1 Answers

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.

like image 75
zangw Avatar answered Nov 09 '22 13:11

zangw