Cara menggunakan mongodb aggregate by date

If you have documents that store dates as Date objects, but you want to return them in a different format, you can use the $dateToString aggregate pipeline operator.

For example, you might want a date to be returned in

db.cats.aggregate(
   [
     {
       $project: {
          name: 1,
          formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
       }
     }
   ]
)
0 format instead of the long
db.cats.aggregate(
   [
     {
       $project: {
          name: 1,
          formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
       }
     }
   ]
)
1 format that includes minutes, seconds, milliseconds, etc

The $dateToString operator converts the Date object to a string, and optionally allows you to specify a format for the resulting output.

Example

Suppose we have a collection called

db.cats.aggregate(
   [
     {
       $project: {
          name: 1,
          formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
       }
     }
   ]
)
3 with the following documents:

{ "_id" : 1, "name" : "Scratch", "born" : ISODate("2021-01-03T23:30:15.123Z") }
{ "_id" : 2, "name" : "Meow", "born" : ISODate("2019-12-08T04:00:20.112Z") }
{ "_id" : 3, "name" : "Fluffy", "born" : ISODate("2020-09-24T10:45:01.007Z") }

We can use $dateToString to return that document with the dates in a different format.

For example, let’s remove the seconds and milliseconds from the date:

db.cats.aggregate(
   [
     {
       $project: {
          name: 1,
          formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
       }
     }
   ]
)

Result:

{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" }
{ "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" }
{ "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }

We were able to format the date by using the

db.cats.aggregate(
   [
     {
       $project: {
          name: 1,
          formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
       }
     }
   ]
)
5 parameter. This is an optional parameter that allows you to use zero or more format specifiers to indicate how the date should be formatted.

See MongoDB $dateToString Format Specifiers for a full list of format specifiers that can be used with the $dateToString operator.

Date in db.cats.aggregate( [ { $project: { name: 1, formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } } } } ] )8 Format

Here’s another example that converts the dates to

db.cats.aggregate(
   [
     {
       $project: {
          name: 1,
          formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
       }
     }
   ]
)
8 format:

db.cats.aggregate(
   [
     {
       $project: {
          _id: 0,
          formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } }
       }
     }
   ]
)

Result:

{ "formattedDate" : "03/01/2021" }
{ "formattedDate" : "08/12/2019" }
{ "formattedDate" : "24/09/2020" }

Date in db.cats.aggregate( [ { $project: { name: 1, formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } } } } ] )0 Format

Or to put it in

db.cats.aggregate(
   [
     {
       $project: {
          name: 1,
          formattedDate: { $dateToString: { format: "%Y-%m-%d %H:%M", date: "$born" } }
       }
     }
   ]
)
0 format, we can simply switch the first two format specifiers around:

db.cats.aggregate(
   [
     {
       $project: {
          _id: 0,
          formattedDate: { $dateToString: { format: "%m/%d/%Y", date: "$born" } }
       }
     }
   ]
)

Result:

{ "formattedDate" : "01/03/2021" }
{ "formattedDate" : "12/08/2019" }
{ "formattedDate" : "09/24/2020" }

Return a Single Date Part

You can include as many or as few format specifiers as you wish. For example, you could use just one format specifier to output just the year part of the date.

Example:

db.cats.aggregate(
   [
     {
       $project: {
          _id: 0,
          formattedDate: { $dateToString: { format: "%Y", date: "$born" } }
       }
     }
   ]
)

Result:

{ "formattedDate" : "2021" }
{ "formattedDate" : "2019" }
{ "formattedDate" : "2020" }

Although, bear in mind that there are other ways to extract just a single date part from a Date object. For example, you can use the

{ "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" }
{ "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" }
{ "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
2 operator to extract the year.

Here are the various operators for extracting each specific date part:

  • { "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" }
    { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" }
    { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
    3
  • { "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" }
    { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" }
    { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
    4
  • { "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" }
    { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" }
    { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
    5
  • { "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" }
    { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" }
    { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
    6
  • { "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" }
    { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" }
    { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
    7
  • { "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" }
    { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" }
    { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
    8
  • { "_id" : 1, "name" : "Scratch", "formattedDate" : "2021-01-03 23:30" }
    { "_id" : 2, "name" : "Meow", "formattedDate" : "2019-12-08 04:00" }
    { "_id" : 3, "name" : "Fluffy", "formattedDate" : "2020-09-24 10:45" }
    9
  • db.cats.aggregate(
       [
         {
           $project: {
              _id: 0,
              formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } }
           }
         }
       ]
    )
    0
  • db.cats.aggregate(
       [
         {
           $project: {
              _id: 0,
              formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } }
           }
         }
       ]
    )
    1
  • db.cats.aggregate(
       [
         {
           $project: {
              _id: 0,
              formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } }
           }
         }
       ]
    )
    2
  • db.cats.aggregate(
       [
         {
           $project: {
              _id: 0,
              formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } }
           }
         }
       ]
    )
    3
  • db.cats.aggregate(
       [
         {
           $project: {
              _id: 0,
              formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } }
           }
         }
       ]
    )
    4
  • db.cats.aggregate(
       [
         {
           $project: {
              _id: 0,
              formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } }
           }
         }
       ]
    )
    5

You can also use the

db.cats.aggregate(
   [
     {
       $project: {
          _id: 0,
          formattedDate: { $dateToString: { format: "%d/%m/%Y", date: "$born" } }
       }
     }
   ]
)
6 operator to return a document that contains all the various date parts separated into their own field.