Johnny John Boy
Johnny John Boy

Reputation: 3202

How to convert and over write MongoDB date string into ISODate using Python PyMongo?

I'm trying to convert a number of document strings in the format "YYYY-MM-DD" into ISODates in MongoDB and have successfully written this for the console which has the desired effect:

db.address.find({ "date" : { $type : 2 } } ).forEach(function(element){  element.date = ISODate(element.date); db.address.save(element);})

I'm trying to do the same in Python something like this:

client = MongoClient(my_mongodb_ip)
db = client.address
result = db.address.find( { "date" : { "$type" : 2 } } );
for r in result:
    print(r['date'])
    r["date"] = datetime.strptime(r["date"], "%Y-%m-%d")
    print(r['date'])
    db.address.update_one({"company": r['company']},
         {"$set": {"date" : r['date']}})

Which I'd like to deliver this:

"date": {
  "$date": "2017-06-28T00:00:00.000Z"
},

I don't get any updates to the DB.

Upvotes: 1

Views: 2768

Answers (3)

Johnny John Boy
Johnny John Boy

Reputation: 3202

I figured out what was going wrong here. No need to import arrow or parser, datetime works just fine:

newest_date = datetime.strptime(r["date"], "%Y-%m-%d")

This creates the new datetime object from my "date" item in my collection assuming the format YYYY-MM-DD. Once I figured that out I had also been incorrectly putting .isoformat() to the end of this creating a string again, no idea why I did that.

With the incorrect isoformat() removed I can now run:

db.address.update_one({"address": "1234", {"$set": {"date" : newest_date}})

And the address will update correctly to type 9, i.e. a date object not a string. I checked this with:

db.address.find({ "date" : { $type : 9 } } )

Upvotes: 0

Ravi kumar
Ravi kumar

Reputation: 330

You can achieve this goal using the arrow module in python. All You need to do is, just create a small function that can take your date as parameter and convert it into the ISO format.

This is how you can do it:

import arrow

def convert_to_ISO_Format(self, value):
    date                          = arrow.get(value)
    date                          = date.format("YYYY-MM-DDTHH:mm:ss")
    date                          = date  + "Z"
    self.converted_date_iso = date

Or If you know the region of the state and you want to convert it accordingly then you can do like this;

    def convert_to_ISO_Region(self,value):
        date                      = arrow.get(value)
        date                      = date.to("Asia/Singapore")
        date                      = date.format("YYYY-MM-DD HH:mm:ss")
        date                      = date  + "Z"
        self.converted_date_iso = date

                 OR

If you want to convert the present date and time into ISO, it's pretty simple like the below one statement.

arrow.utcnow().datetime

I hope this can be helpful to your problem.

Upvotes: 0

Clément
Clément

Reputation: 1256

Try to use:

import dateutil.parser

dateutil.parser.parse(<your time String>)

Upvotes: 2

Related Questions