MongoDB based on time aggregate example

Source: Internet
Author: User

You need to add translatefields values to the following collection according to Lastupdate by day.

Rs_test:secondary> db.new_result.find ();   {  "_id"  : objectid (" 57fb0756e31f84a56ed41889 "), " Lastupdate " : isodate (" 2016-09-02t01:35:02.471z "), " Translatefields " : 9 }    { " _id " : objectid (" 57fb0756e31f84a56ed4188a "), " Lastupdate " : isodate (" 2016-09-05t11:13:28.344z "), " Translatefields " : 10 }    { " _id " : objectid (" 57fb0756e31f84a56ed4188b "), " Lastupdate " : isodate (" 2016-09-05t09:26:41.016z "), " Translatefields " : 33 }    { " _id " : objectid (" 57fb0756e31f84a56ed4188c "), " Lastupdate " : isodate (" 2016-09-02t13:34:50.114z "), " Translatefields " : 12 }    { " _id " : objectid (" 57fb0756e31f84a56ed4188d "), " Lastupdate " : isodate (" 2016-08-26t03:49:52.369z "), " TranslaTefields " : 17 } 


If it is in SQL Server, the grouping statistics should be written like this:

SELECT CONVERT (varchar,lastupdate,112), SUM (translatefields) from Dbo.new_result GROUP by CONVERT (Varchar,lastupdate, ) ORDER by 1;


So in MongoDB, there are 3 ways to aggregate: Group, aggregate, and MapReduce.

2.6 Version Aggregate method    db.new_result.aggregate (           {             $group  : {                _id : {  year: {  $year:  "$LastUpdate"  }, month: {  $month:  "$LastUpdate"  }, day: {  $dayOfMonth:  "$LastUpdate"  } },                totalTime: {  $sum:  "$ Translatefields " }            }           },           {             $sort  : {                 "_id.year": 1,  "_id.month": 1,  "_id.day": 1             }           }    )

3.0 version Aggregate method    db.new_result.aggregate (           {             $group  : {                yearmonthday: {   $dateToString: { format:  "%y-%m-%d", date:  "$LastUpdate"  } },                totaltime: { $ sum:  "$TranslateFields"  }            }           },           {             $sort  : {                 "Yearmonthday":  1             }          }     )

The group method Db.new_result.group ({keyf:function (doc) {var date = new Date (DOC).      Lastupdate);      var DateKey = "" +date.getfullyear () + "-" + (Date.getmonth () +1) + "-" +date.getdate ();    return {' Day ':d Atekey}; }, initial: {"Time": 0}, Reduce:function (Doc, prev) {prev.time + = doc.        Translatefields;    }, Finalize:function Finalize (out) {return out; }    }    });
First Save As Date     //1    db.tmp_result.find ({"Value"). Status ": 3},{" value. Translatefields ": 1," value. Lastupdate ": 1}". ForEach (        function (item) {              db.new_result.save ({"LastUpdate": Item.value.LastUpdate.getFullYear () + "-" +                   (Item.value.LastUpdate.getMonth () +1) + "-" +                  item.value.lastupdate.getdate (),                   " Translatefields ": Item.value.TranslateFields});        }      )     //2    db.new_result.aggregate (           {             $group  : {                _id: "$LastUpdate",                totaltime: { $ sum:  "$TranslateFields"  }            }           }          ,{"$sort": {"_id":1}}    )

For the aggregate method, it is best to $match before $group, reducing the amount of data, and if the filtered key is indexed, the query will also go through the index.

Db. Translateticket.aggregate (    {         "$match":         {               "lastupdate":  {"$gte": Isodate ("2016-06-19t00:00:00.000z"),  "$lt": Isodate (" 2016-09-19t00:00:00.000z ")},             " Status ": 3        }    },     {         "$group":         {          _id : { month: { $ month:  "$LastUpdate"  }, day: {  $dayOfMonth:  "$LastUpdate"  }, year:  {  $year:  "$LastUpdate"  } },          totaltime: {  $sum: "$CharactersCount"  }        }    },     {         "$sort":         {             "_id.year": 1, "_ Id.month ": 1," _id.day ":1        }    }     )

In this case, it is best to create the following index:

Db. Translateticket.createindex ({"Lastupdate": 1, "Status": 1},{background:1})


This article is from the SQL Server deep Dive blog, so be sure to keep this source http://ultrasql.blog.51cto.com/9591438/1861003

MongoDB based on time aggregate example

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.