I keep getting the following error when trying to update localTimestamp:
MongooseError [CastError]: Cast to date failed for value "{
'$cond': {
if: { '$eq': [Array] },
then: 2023-11-19T14:25:30.788Z,
else: '$localTimestamp'
}
}" at path "localTimestamp"
However, if i use timestamp directly, it works (commented out)
Since you want to use $cond in the update query, you have to wrap you update config in the array, which would allow you to use aggregate options in the update query:
Note: Make sure that exampleArray field exists on the document and is of type array. Otherwise, the query will throw the error because you would try to calculate the $size of the non-array field.
The code works perfectly. The else: clauses specify $handshakeTimestamp and $localTimestamp respectively. Since $handshakeTimestamp does not exist in the source document, you get undefined. As for $localTimestamp it is undefined in the original documents and that is what you get in the results.
It looks like Mongoose is in your way and prevents you from doing something that can be done with the native driver. I would try to do this update with the native API rather than the Model object you got from your Schema.