disruptive
disruptive

Reputation: 5946

create unique id in mongodb from last inserted id using pymongo

Is there a way I can find the last inserted document and the field, i.e. _id or id such that I can increment and use when inserting a new document?

The issue is that I create my own id count, but I do not store this, now I've deleted records, I cannot seem to add new records because I am attempting to use the same id.

Upvotes: 0

Views: 485

Answers (1)

lesingerouge
lesingerouge

Reputation: 1168

There is no way to check insertion order in MongoDB, because the database does not keep any metadata in the collections regading the documents.

If your _id field is generated server-side then you need to have a very good algorithm for this value in order to provide collision avoidance and uniqueness while at the same time following any sequential constraints that you might have.

Upvotes: 1

Related Questions