Malla
Malla

Reputation: 1

Python: How do I provide my own ObjectID / _id when I insert into a MongoDB?

I'm inserting information regarding some files into a MongoDB, among other things a checksum for the file. I want to use the checksum as the _id for the document. How do I do that?

Upvotes: 0

Views: 1059

Answers (1)

James Wahlin
James Wahlin

Reputation: 2821

You would set the _id field explicitly on the document you are storing. For example:

document = {}
document['_id'] = my_id

Please note that the _id value has to be unique for every document. Only store checksum as _id if you know that it will be unique.

Upvotes: 3

Related Questions