Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding createdAt and updatedAt in pymongo

I want to add auto generated field created_at and updated_at in mongodb in python using pymongo.

This functionality is provided in Javascript when creating mongo schema using

var yourSchema = new Schema({..}, { timestamps: { createdAt: 'created_at', updatedAt: 'updated_at'} });

How exactly can this be done in pymongo?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is a mongoose feature, not to be confused with javascript or MongoDB.
pymongo does not offer such capabilities.

You will just have to manually specify them in your operations, for example:

from datetime import datetime

db.collection.updateOne({
   "key": 5
},
{
  "$set": { "updated_at": datetime.now() },
  "$setOnInsert": { "created_at": datetime.now() }
}, upsert=True)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading