Unable to use aggregate with date hour

I need to shoot out, but I had a play, I’m sure someone can simplify this massively as opposed to the monstrosity below, but you end up with the ID of the document and the actual date that’s needed in a field, in your case this should be the name of the field you want to update.
You can then add a final $merge statement to merge back into the collection, updating that field.

db.getCollection("Test").aggregate([
{
$match: {
  $expr: {
    $eq: [
      {
        $type: "$myDate"
      },
      "object"
    ]
  }
}
},
{
    $addFields:{
        'myDate._id':'$_id'
    }
},
{
    $replaceRoot:{
        newRoot:'$myDate'
    }
},
{
    $addFields:{
        actualValue:{
            $getField:{
                $literal:'$date'
            }
        }
    }
},
{
    $project:{
        'myDate':{$toDate:'$actualValue'}
    }
}
])

(The weirdness with $replaceRoot was due to getting a syntax error with $getField when querying a nested element)

Maybe this is a copy/paste error but " ‘$x’ " is a syntax error that converts $x to literally ‘$x’ which is a string

Good evening Pros,

yesterday i was not able to continue the conversation due to post limit,
after running the above query, i got the below error

Invalid $addFields :: caused by :: Unrecognized expression ‘$getField’

i have changed the query as like this

db.getCollection('sample_data')
    .aggregate([
        {
            $match: {
                $expr: {
                    $eq: [
                        {
                            $type: '$updated_at',
                        },
                        'object',
                    ],
                },
            },
        },
        {
            $addFields: {
                'updated_at._id': '$_id',
            },
        },
        {
            $replaceRoot: {
                newRoot: '$updated_at',
            },
        },
        {
            $addFields: {
                actualValue: {
                    $getField: {
                        $literal: '$date',
                    },
                },
            },
        },
        {
            $project: {
                updated_at: { $toDate: '$actualValue' },
            },
        },
    ]);

What Mongo server version are you running? Pre 5?

yes mongodb version is 4.4.10

db.sample_data.aggregate([
{
$match: { business_key: ‘Hourlyprocess_JWIL_67hh3jj0’ }
},
{
$project: {
_id: 1,
loginTime: ‘$bom_data.loginTime’,
description: ‘$bom_data.description’,
business_key: 1,
taskid: 1,
hour: { $hour: { date: ‘$updated_at’ } },
updated_at: 1
}
},
{
$group: {
_id: {
hour: ‘$hour’, // Group by hour…
business_key: ‘$business_key’
},
count: { $sum: 1 },
data: { $push: ‘$$ROOT’ }
}
},
{
$sort: { ‘_id.hour’: 1 }
}
]);