MongoDB Learning-Advanced query [aggregation Group]

Source: Internet
Author: User
Tags prev

Group It takes about a few parameters.

    1. Key: The field used to group the document. And Keyf both must have a
    2. KEYF: You can accept a JavaScript function. The field used to dynamically determine the grouped document. And key both must have a
    3. Initialization of variables used in Initial:reduce
    4. Reduce: The reduce function that is executed. The function needs to return a value.
    5. Cond: The condition to perform the filtering.
    6. Finallize: At reduce execution completion, the result set returns the function that was eventually executed against the result set. An optional

Test data

    Test Data    Db.stocks.insert ({"Day": "2010/10/03", "Time": "2010/10/03 03:47:01 GMT-400", "Price": 4.23});      Db.stocks.insert ({"Day": "2010/10/04", "Time": "2010/10/04 11:28:39 GMT-400", "Price": 4.27});      Db.stocks.insert ({"Day": "2010/10/03", "Time": "2010/10/03 05:00:23 GMT-400", "Price": 4.10});      Db.stocks.insert ({"Day": "2010/10/06", "Time": "2010/10/06 05:27:58 GMT-400", "Price": 4.30});      Db.stocks.insert ({"Day": "2010/10/04", "Time": "2010/10/04 08:34:50 GMT-400", "Price": 4.01});  

Test the Group program code to be executed

Db.runcommand ({"group": {      "ns": "Stocks", "      Key": "Day",      "initial": {"Time": 0},      "$reduce": function ( Doc, prev) {          if (Doc.time > Prev.time) {              prev.price = Doc.price;              Prev.time = Doc.time;          }      }  

Results after the execution

    {"          retval": [              {                  "time": 0              }          ],          "Count": 5,          "keys": 1,          "OK": 1      }  

MongoDB Learning-Advanced query [aggregation Group]

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.