The group in MongoDB

Source: Internet
Author: User

In MongoDB's query, there is a group function similar to the group by feature in SQL. The functions of the two are similar, but the differences are quite obvious.

For SQL, the role of group by IS to install the records in the data table into groups according to the columns, and the query results can only select the columns that are contained in group by, or use the aggregate function (sum,count,avg,max,min) Other columns. And you can use aggregate functions only on columns on numeric types.

However, in MongoDB, we can display columns in the results that are neither group by nor aggregate functions. In this way, we can get more detailed information within a group. And in SQL to do this, you need to use the sub-query and other sophisticated means to do so.

MongoDB's group function, more like a template method that defines the various intermediate steps, is used by the user as a function or other form to participate in the intermediate steps. First, the key property is used to specify the basis column for the grouping. As for the True/false parameter after the column name, There is an introduction that is used to decide whether to show the column names of the basis columns for each grouping. However, there is no difference in the version that I am currently using, and it is generally set to true. After that, it is the steps to start the execution.

    1. Initial. Initialize the accumulator for the grouping. If we need columns in the result that are outside of the group dependency column, you can put the columns in a grouped accumulator. The so-called accumulator, can be an object or an array. Each grouping will have an accumulator object.
    2. $reduce. Called when a record is added to a group. The callback function is in the form of function (Cur,perv). Where cur is the object of the current record, Perv is the group document object the last time the $reduce function was executed.
      • This function is a key function to add additional information. A grouped document object that contains all the information for a grouping, the accumulator we declare in initial, is a property of that object, and we can add any property to that object, and all of its properties are displayed as results.
      • Because each grouping has a Document object, it is convenient to record the details of the grouping in the Document object. The recorded information is stored on the Cur object (the object is the map of the data row and contains the values of each data column).
    3. Finalize. Called when a packet has been processed. The callback function is in the form of function (DOC). The parameter doc is the document object for the grouping. If we need to do a statistic grouping of records such as Count, it can be written within the function.

Now, use MS SQL and MongoDB's actual gourp operation to illustrate the difference.

First, create the same table (the table named person) in MS SQL and MongoDB, and insert the same data.

    • Now we are asking for a gender grouping.
      1. In SQL
      2. In MongoDB
    • Next, you need to know the number of people in each group.
      1. In SQL.
      2. In MongoDB
    • The name of each person in the group is queried, and in SQL, there are other ways to demonstrate only the practices in MongoDB.
    • Also record the number of people in the group, and the name of your wife. There are two ways to achieve this.
      •  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.