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?

>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

   "key": 5
  "$set": { "updated_at": },
  "$setOnInsert": { "created_at": }
}, upsert=True)

Leave a ReplyCancel reply