I have a collection of errors, so that every error carries a date
field. How can I aggregate/count/group the errors by DAY only (i.e. exclude the time of the day)? I guess, some smart projection should be applied.
$project − Used to select some specific fields from a collection. $match − This is a filtering operation and thus this can reduce the amount of documents that are given as input to the next stage.
Definition. Evaluates a boolean and returns the opposite boolean value; i.e. when passed an expression that evaluates to true , $not returns false ; when passed an expression that evaluates to false , $not returns true .
$unwind treats the sizes field as a single element array if: the field is present, the value is not null, and. the value is not an empty array.
You can do this by using the following aggregation operators:
This gives you the error count for each date:
db.errors.aggregate( { $group : { _id: { year : { $year : "$date" }, month : { $month : "$date" }, day : { $dayOfMonth : "$date" }, }, count: { $sum: 1 } }} );
This example assumes that the date field in your error documents is date
and of type BSON Date. There is also a Timestamp type in MongoDB, but use of this type is explicitely discouraged by the documentation:
Note: The BSON Timestamp type is for internal MongoDB use. For most cases, in application development, you will want to use the BSON date type. See Date for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With