Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a dollar sign mean in mongodb in terms of groups?

Tags:

According to the docs, a "$" is reserved for operators. If you look at the group operator however, values need to have a dollar prefixed. These values are not operators. What does it mean in this context then? Example below:

db.article.aggregate(     { $group : {         _id : "$author",         docsPerAuthor : { $sum : 1 },         viewsPerAuthor : { $sum : "$pageViews" }     }} ); 

Why does pageViews need a leading dollar sign? I've tried it locally and it doesn't work without the dollar sign.

like image 551
user1099123 Avatar asked Apr 28 '13 15:04

user1099123


People also ask

What does dollar sign do in MongoDB?

In expression, the dollar sign $ evaluates to a field path; i.e. provides access to the field. For example, the $eq expression $eq: [ "$price", "$1" ] performs an equality check between the value in the field named price and the value in the field named 1 in the document.

What is $$ root in MongoDB?

The $$ROOT variable contains the source documents for the group. If you'd like to just pass them through unmodified, you can do this by $pushing $$ROOT into the output from the group.

What is Groupby in MongoDB?

MongoDB group by is used to group data from the collection, we can achieve group by clause using aggregate function and group method in MongoDB. While using aggregate function with group by clause query operations is faster as normal query, basically aggregate function is used in multiple condition.

What are the aggregation expressions used in MongoDB?

$match: It is used for filtering the documents can reduce the amount of documents that are given as input to the next stage. $project: It is used to select some specific fields from a collection. $group: It is used to group documents based on some value. $sort: It is used to sort the document that is rearranging them.


1 Answers

In this case "$string" means you want to use the value of the key named "string" in the processed document. Contrast with "string" which would be a literal string.

like image 74
Asya Kamsky Avatar answered Nov 09 '22 23:11

Asya Kamsky