0

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

3
  • It seems like you are doing an update here. Am I correct in assuming this? Commented Nov 13, 2020 at 19:59
  • yes , first i filter only the docs where date_created is string and then save the doc parsed Commented Nov 13, 2020 at 20:04
  • You can just do an update like so db.shipping.updateMany({'dateCreated': {'$type':"string"}}, {'$set': {"dateCreated": new Date('$dateCreated')}}) Also what error are you getting when you try to execute what you have using pymongo? Commented Nov 13, 2020 at 20:10

1 Answer 1

1

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'))}})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.