5 Ways to Get the Minutes from a Date in MongoDB

This article presents 5 ways to return the minutes portion from a Date object in MongoDB.

Sample Data

Suppose we have a collection called cats 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:47Z") }
{ "_id" : 3, "name" : "Fluffy", "born" : ISODate("2020-09-24T10:45:00Z") }

The following examples demonstrate various options for returning the minutes portion from the born field of those documents.

The $minute Operator

The $minute operator is specifically designed to return a document with the minutes portion of a given date.

We can run the following code to return the minutes portion from the born field in the above document.

db.cats.aggregate(
  [
    {
      $project:
        {
          _id: 0,
          birthMinute: { $minute: "$born" }
        }
    }
  ]
)

Result:

{ "birthMinute" : 30 }
{ "birthMinute" : 0 }
{ "birthMinute" : 45 }

You can also specify the timezone when using the $minute operator.

See MongoDB $minute for more information and examples.

The $dateToString Operator

The $dateToString operator converts a date object to a string according to a user-specified format. The user can therefore specify that just the minutes portion is returned if required.

There are format specifiers for each date part. The %M format specifier returns the minutes portion.

Example:

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

Result:

{ "birthMinute" : "30" }
{ "birthMinute" : "00" }
{ "birthMinute" : "45" }

You can also use %Z to return the minutes offset from UTC as a number.

See MongoDB $dateToString for more information and examples.

The $dateToParts Operator

The $dateToParts operator returns a document that contains the constituent parts of a given BSON Date value as individual properties. The properties returned are year, month, day, hour, minute, second and millisecond.

We could therefore use $dateToParts in one pipeline stage, then add another pipeline stage that extracts the minute part.

Here’s what $dateToParts returns for our three documents:

db.cats.aggregate(
  [
    {
      $project:
        {
          _id: 0,
          dateParts: { $dateToParts: { date: "$born" } }
        }
    }
  ]
).pretty()

Result:

{
	"dateParts" : {
		"year" : 2021,
		"month" : 1,
		"day" : 3,
		"hour" : 23,
		"minute" : 30,
		"second" : 15,
		"millisecond" : 123
	}
}
{
	"dateParts" : {
		"year" : 2019,
		"month" : 12,
		"day" : 8,
		"hour" : 4,
		"minute" : 0,
		"second" : 47,
		"millisecond" : 0
	}
}
{
	"dateParts" : {
		"year" : 2020,
		"month" : 9,
		"day" : 24,
		"hour" : 10,
		"minute" : 45,
		"second" : 0,
		"millisecond" : 0
	}
}

This result can then be passed to the next stage in the pipeline to extract just the minute field.

Here’s what happens if we add another projection for just the minute field:

db.cats.aggregate(
  [
    {
      $project:
        {
          _id: 0,
          dateParts: { $dateToParts: { date: "$born" } }
        }
    },
    {
      $project:
        {
          birthMinute: "$dateParts.minute"
        }
    }
  ]
)

Result:

{ "birthMinute" : 30 }
{ "birthMinute" : 0 }
{ "birthMinute" : 45 }

So if you use $dateToParts in your pipeline, you will then have access to the minute field (and all other fields) in the next stage.

See MongoDB $dateToParts for more information and examples.

The forEach() Method

You can use cursor.forEach() to iterate through the cursor, using a JavaScript method such as getMinutes() or getUTCMinutes() to return just the minutes.

db.cats.find().forEach(
  function(c) {
    print(
      c.born.getUTCMinutes()
      );
  }
);

Result:

30
0
45

The getUTCMinutes() JavaScript method returns an integer number, between 0 and 59, representing the minutes in the given date according to universal time.

The getMinutes() method returns it in local time.

You may also notice that the previous options return a whole document that contains a name/value pair, whereas this option returns just the actual minutes value, and not the whole document.

The map() Method

The cursor.map() method applies a function to each document visited by the cursor and combines the values into an array.

Example:

db.cats.find().map(
  function(c) {
    c = c.born.getUTCMinutes();
    return c;
  }
);

Result:

[ 30, 0, 45 ]