Joseph
Joseph

Reputation: 604

Importing using MongoDB compass using a json file, dates are shown as strings

I want to insert a date into a MongoDB collection. I am importing my data from a JSON file which I am generating from a python script. The script inserts the date as a string, which is not idea. I have found how to use "date": {$date: "2019-10-11"} if I was inserting via MongoDB document to insert the date as date, but can't see how to do this in python.

So is there a way to change all the "date" fields to a date in my collection (which is called collisions?)

Just to add to this: The image shows what I see in MongoDB compass, is there any way to change the String type on date to ISOdate? [![image][1]][1]

Sample JSON output (single record)

{
    "date": "2019-10-11",
    "borough": "QUEENS",
    "weekday": 5,
    "year": 2019,
    "month": 10,
    "day": 11
}


  [1]: https://i.sstatic.net/6PAul.png

Upvotes: 1

Views: 957

Answers (1)

Joseph
Joseph

Reputation: 604

As it turns out the answer was in stackoverflow all along.

Using MongoShell, the following command would work, I was first confused by db.collection as I didn't realise "collection" needed to be the collection you wanted to affect:

db.<COLLECTION NAME>.updateMany(
  {},
  [{ "$set": { "<FIELD TO CHANGE": { "$toDate": "$<FIELD TO CHANGE" } }}]
);

Upvotes: 3

Related Questions