How to get the object id in PyMongo after an insert?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In PyMongo, the usual way to get the inserted document's _id is to read result.inserted_id after insert_one(). If you insert multiple documents, use result.inserted_ids. That is the normal, supported API for retrieving the generated object IDs after an insert.
Single Insert with insert_one
When you insert one document, insert_one() returns an InsertOneResult object.
result.inserted_id is the simplest and most direct answer to the question.
If the document did not already contain an _id, PyMongo creates one automatically before sending the insert to MongoDB.
The Original Document Also Gets an _id
A detail many developers miss is that PyMongo usually mutates the inserted document object by adding _id if it was missing.
Those two values should refer to the same inserted identifier. In day-to-day code, result.inserted_id is clearer because it makes the source of the value obvious, but it is useful to know why the original dictionary appears to change.
Inserting Many Documents
For bulk inserts, insert_many() returns an InsertManyResult with a list of IDs.
This preserves the order of the inserted documents as represented by the result.
Custom _id Values
MongoDB does not force you to use the default ObjectId. If you provide your own _id, PyMongo uses it.
This prints user-42. The important rule is that _id must be unique in the collection.
Convert the ID to a String Only When Needed
The inserted ID is often an ObjectId instance, not a plain string. That is fine, and you usually should keep it in that form until you actually need a string representation for JSON or logging.
Keeping the original type is useful because PyMongo queries accept ObjectId directly.
Using the Returned ID Immediately
One common follow-up is to use the inserted ID right away in another operation, such as a verification read or a redirect target in an API response.
That works without any extra conversion because the returned value is already in the type PyMongo expects for _id lookups.
Handle Errors Explicitly
Insert operations can fail because of connectivity issues, validation rules, or duplicate _id values.
This matters most when you assign custom _id values yourself or perform inserts into collections with additional unique constraints.
Common Pitfalls
The most common mistake is looking for the inserted ID on the collection object instead of on the result object returned by insert_one() or insert_many(). Another is converting every ObjectId to a string immediately and then forgetting that later queries may expect an ObjectId again. Developers are also sometimes surprised that the original document dictionary gets an _id added to it when one was not provided explicitly.
Summary
- Use
result.inserted_idafterinsert_one(). - Use
result.inserted_idsafterinsert_many(). - PyMongo also adds
_idto the original document object if it was missing. - Custom
_idvalues are allowed as long as they are unique. - Keep the returned value as an
ObjectIdunless you specifically need a string form.
Related reading
- How to get the position of a character in Python?
- How to get the Transaction ID or relate message with Dialogic DSI library in C?
- How to get total number of pages in DynamoDB if we set a limit?
- How to Get True Size of MySQL Database?
- How to get the parents of a Python class?
- How to get the path and name of the Python file that is currently executing?
- How to get value of a FLAG in ClickHouse
- How to grant all privileges to root user in MySQL 8.0

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.