Matias Ordoñez
Matias Ordoñez

Reputation: 105

Execute mongodb command on pymongo

Hello : i have the following script on mongodb :

db.shipping.find({"dateCreated":{$type:"string"}}).forEach(
function(doc){
doc["dateCreated"]=new Date(doc["dateCreated"]);
db.shipping.save(doc);
})

I need to execute in pymongo

Its a simple function to parse strings to date and i want to add this task to apache airflow

Regards

Upvotes: 0

Views: 398

Answers (1)

Belly Buster
Belly Buster

Reputation: 8814

The equivalent of your code in pymongo would be:

from pymongo import MongoClient
from dateutil.parser import parse

db = MongoClient()['mydatabase']

for doc in db.shipping.find({'dateCreated':{'$type':'string'}}):
    db.shipping.update_one({'_id': doc.get('_id')}, {'$set': { 'dateCreated': parse(doc.get('dateCreated'))}})

Upvotes: 1

Related Questions