Martin
Martin

Reputation: 1

How can you use multiple ids in mongo aggregate $group when performing $dateToString on one of the ids

I want to use $dateToString so that my data is split by each day and i also want to group by the lognames.

Grouping by date and logname works if i do not use $dateToString but then each entry is split by each specific time as the dates are stored in mongos default format ( "2020-04-21T02:06:26.139Z"

The problem seems to be when trying to use $dateToString to convert the date while having another id

This works on its own (groups by each day)

Entry.aggregate(
    [{                
          $group: {
                    _id: { $dateToString: { format: "%d-%m-%Y", date: "$date" } },
},},],

This is what i want to do but it does not work

Entry.aggregate(
    [{                
          $group: {
                 _id: { $dateToString: { format: "%d-%m-%Y", date: "$date" }, logname: "$logname"}, 

},},],

This works without the above $date conversion (groups by each specific time and logname)

Entry.aggregate(
    [{                
            $group: {
                   _id: { date: "$date", logname: "$logname" },                    
},},],

The error is

"errmsg": "An object representing an expression must have exactly one field: { $dateToString: { format: \"%d-%m-%Y\", date: \"$date\" }, logname: \"$logname\" }",

Upvotes: 0

Views: 542

Answers (1)

Martin
Martin

Reputation: 1

I just needed to name the formated date (named dateFormated)

_id: { dateFormated: { $dateToString: { format: "%d-%m-%Y", date: "$date" }}, logname: "$logname"}, 

Upvotes: 0

Related Questions