Mongo date format


I want to change the date type from dateString to date field ( createdAt to updatedAt) how to do it ???

when I go to compas, createdAt is a date string, when I manually change to date and export the record, I see that the updatedAt field is a date NumberLong.

how to change the type of createdAt on all my collection ??

Hello @tim_Ran ,

Welcome to The MongoDB Community Forums! :wave:

I notice you haven’t had a response to this topic yet - were you able to find a solution?
If not, I assume you wanted to convert the datatype of the field createdAt from string to ISODate, you can update below query as per your use case.

db.collection.updateMany(
                     {"createdAt": 
                      {$type:"string"}
                     },
                     [{$set: 
                       {"createdAt": 
                         {$toDate : "$createdAt"}
                       }}]
)

Explanation:

  • db.collection.updateMany() will updates all documents that match the specified filter for a collection
  • $type will help check which documents in whole collection have “createdAt” as a String Data type
  • $set operator replaces the value of a field with the specified value.
  • $toDate Converts a value to a date. If the value cannot be converted to a date, $toDate errors. If the value is null or missing, $toDate returns null.

Regards,
Tarun

2 Likes

Hello @tim_Ran
To change the data type of the createdAt field from a string to a date in MongoDB, you can use the $convert operator in the updateMany() method. This operator allows you to convert a field to a specified data type using an expression.

Here’s an example of how you can use the $convert operator to change the createdAt field to a date type:

db.collection.updateMany(
  {},
  { $convert: { input: "$createdAt", to: "date" } }
);

This will update all documents in the collection collection and set the createdAt field to a date type.

It’s important to note that you should always make a backup of your data before making any changes. Also, you should test your queries on a small subset of data before running it on your entire collection, to ensure that it behaves as expected.

If you want to rename createdAt to updatedAt in the same operation you can use the $rename operator

db.collection.updateMany(
  {},
  [
    {
      $convert: {
        input: "$createdAt",
        to: "date"
      }
    },
    { $rename: { "createdAt": "updatedAt" } }
  ]
);

This will update all documents, change the createdAt field to a date and rename it to updatedAt

It’s important to note that this will change the type of the field in the documents but it won’t change the index type, if you have any index on that field, you will need to remove it and re-create it. Also, if you’re using any code that depends on the original type of the field, you’ll need to update it to reflect the new data type.

1 Like

Thanks for your feeldback @Tarun_Gaur
i want to convert date string to date Long (Number long) not to ISO date

If you do it the way @Tarun_Gaur suggests and then do the mongoexport you will end up with what you expect.

Example:

db.foo.drop();
db.foo.insertOne({createdAt:"2023-01-19T08:24:14-05:00"})
db.foo.updateMany({createdAt:{$type: "string"}},[{$set:{createdAt:{$toDate:"$createdAt"}}}])
mongoexport -d test -c foo --quiet --pretty -jsonFormat=canonical
{
	"_id": {
		"$oid": "63c948e23db2dd3bf3ec900c"
	},
	"createdAt": {
		"$date": {
			"$numberLong": "1674134654000"
		}
	}
}

3 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.