I want MongoDB query to minus a day from existing date in a column and update the same.
query to update a date like
original date = ‘2024-12-24T18:30:32’
updated date = ‘2024-12-23T18:30:32’
and how to execute the query on terminal also….?
I want MongoDB query to minus a day from existing date in a column and update the same.
query to update a date like
original date = ‘2024-12-24T18:30:32’
updated date = ‘2024-12-23T18:30:32’
and how to execute the query on terminal also….?
2
Answers
You can use a pipeline update to achieve this, like so:
Mongo Playground
Go to the terminal and use
mongosh
command to login to DB as below:Connect to appropriate DB using
show dbs
anduse <db_name>
command. And use$dateSubtract
expression as below:Syntax in general:
Refer below links for more details:
https://www.mongodb.com/docs/v4.4/mongo/#start-the-mongo-shell-and-connect-to-mongodb
https://www.mongodb.com/docs/manual/reference/operator/aggregation/dateSubtract/