How to insert document in MongoDB with custom object id in Python

  mongodb, pymongo, python

I am inserting a document in MongoDB collection using below code:

try:
    x = coll.insert_one(data_dict).inserted_id
    print(x)
except Exception as e:
    log.error("Exception at saving data for db {}".format(cid))
    log.error(e)

This inserts document in collection and mongo db creates its own object id as shown in below image:

enter image description here

_id is automatically created. Is it possible to insert your own id here. How can we do that. Thanks

Source: Python Questions

LEAVE A COMMENT