MongoDB $isoWeek

In MongoDB, the $isoWeek aggregation pipeline operator returns the week number in ISO 8601 format, ranging from 1 to 53.

When using the $isoWeek operator, you can optionally specify a timezone to use for the result.

The $isoWeek operator accepts either a date (as either a Date, a Timestamp, or an ObjectId), or a document that specifies the date and timezone to use.

Read more

MongoDB $isoDayOfWeek

In MongoDB, the $isoDayOfWeek aggregation pipeline operator returns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday).

You can optionally specify a timezone to use for the result.

The $isoDayOfWeek operator accepts either a date (as either a Date, a Timestamp, or an ObjectId), or a document that specifies the date and timezone to use.

Read more

MongoDB $dayOfWeek

In MongoDB, the $dayOfWeek aggregation pipeline operator returns the day of the week for a date as a number between 1 (Sunday) and 7 (Saturday).

You can optionally specify a timezone to use for the result.

The $dayOfWeek operator accepts either a date (as either a Date, a Timestamp, or an ObjectId), or a document that specifies the date and timezone to use.

Read more

MongoDB $dayOfMonth

In MongoDB, the $dayOfMonth aggregation pipeline operator returns the day of the month for a date as a number between 1 and 31.

You can optionally specify a timezone to use for the result.

The $dayOfMonth operator accepts either a date (as either a Date, a Timestamp, or an ObjectId), or a document that specifies the date and timezone to use.

Read more

MongoDB $dayOfYear

In MongoDB, the $dayOfYear aggregation pipeline operator returns the day of the year for a date as a number between 1 and 366.

You can optionally specify a timezone to use for the result.

The $dayOfYear operator accepts either a date (as either a Date, a Timestamp, or an ObjectId), or a document that specifies the date and timezone to use.

Read more

$unionWith – MongoDB’s Equivalent of UNION ALL

If you’re familiar with SQL, you might know about the UNION clause, which concatenates the results of two queries into a single result set. In particular, UNION ALL includes duplicates.

In MongoDB, we can use the $unionWith aggregation pipeline stage to achieve the same effect that UNION ALL produces. The $unionWith stage performs a union of two collections – it combines pipeline results from two collections into a single result set. And it includes duplicates.

Read more

MongoDB $sortByCount Aggregation Operator

In MongoDB the $sortByCount aggregation pipeline stage groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.

Each group is output in its own document, which consists of two fields:

  • an _id field containing the distinct grouping value, and
  • count field containing the number of documents belonging to that grouping.

The documents are sorted by count in descending order.

Read more