I’m new to MongoDB and looking for some guidance on how to achieve a trigger that will update the datatype for a specific field when the new record is added.
We are in need of converting a field $TimeSubmitted_UTC from a string to $toDate. I understand sending and storing a date time field as a string is not optimal, though this has been the process for the past 7 years so not certain how quickly I will be able to convince them of changing the datatype of the source file.
I have been able to update the existing data in the collection through an aggregation pipeline and/or through the shell, though have run into a stumbling block when attempting to get this to work on new data being submitted. We have access to Atlas and I began reading up on Triggers, though am at a loss on how to utilize the code that was successfully used on the per-existing data on new data that arrives.
db.students.aggregate([
{
$addFields: {
timestamp: {
$toDate: "$timesubmitted_utc"
}
}
},
{
$merge: "students"
}
])
In the above example, I had it create a new field, have also run the code to replace the current field.
db.students.aggregate([
{
$addFields: {
tiimesubmitted_utc: {
$toDate: "$timesubmitted_utc"
}
}
},
{
$merge: "students"
}
])
Apologies on what I hope is a simple question and appreciate any help.