Advanced Operations for MongoDB (aggregation framework)

Source: Internet
Author: User

Group BY query do not use the Java drive with the group by, to use the 2.2 version of the aggregate aggregation framework, after the test speed of one times faster

Reference

Official website: http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/
Example: http://www.yeetrack.com/?p=649


Before viewing the example, please look at the 2 reference documents, especially the official website


Example

[Java]View Plaincopy
    1. First, $match filters out where conditions
    2. Basicdbobject[] Array = {
    3. New Basicdbobject ("Starttimelong", new Basicdbobject ("$gte", BeginTime)),
    4. New Basicdbobject ("Starttimelong", new Basicdbobject ("$lt", EndTime)};
    5. Basicdbobject cond = new Basicdbobject ();
    6. Cond.put ("$and", array);
    7. DBObject match = new Basicdbobject ("$match", cond);
    8. Use $project to assemble the data required by the group, including OptCode columns, processtime columns
    9. DBObject fields = new Basicdbobject ("OptCode", 1); //Interface
    10. Fields.put ("Processtime", 1); Time consuming
    11. Fields.put ("Provincecode", 1); Provinces
    12. Fields.put ("Channelsubcode", 1); Channel
    13. Fields.put ("Platformcode", 1); Platform
    14. DBObject project = new Basicdbobject ("$project", fields);
    15. Grouping with $group
    16. DBObject _group = new Basicdbobject ("Provincecode", "$provinceCode");
    17. _group.put ("Channelsubcode", "$channelSubCode");
    18. _group.put ("Platformcode", "$platFormCode");
    19. _group.put ("OptCode", "$optCode");
    20. DBObject groupfields = new Basicdbobject ("_id", _group);
    21. Total
    22. Groupfields.put ("Count", new Basicdbobject ("$sum", 1));
    23. //Sum
    24. Groupfields.put ("processtime_sum", new Basicdbobject ("$sum","$processTime"));
    25. DBObject Group = new Basicdbobject ("$group", groupfields);
    26. Aggregationoutput output = Mongodbutil.getloginfocollection (). Aggregate (match, project, group);

Advanced Operations for MongoDB (aggregation framework)

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.