Sling Academy
Home/Python/PyMongo: How to insert a document and get the ID

PyMongo: How to insert a document and get the ID

Last updated: February 06, 2024

Introduction

Working with MongoDB in Python is an essential skill for backend developers and data scientists alike. PyMongo, the official Python driver for MongoDB, makes it easy to work with MongoDB databases. One of the common tasks when managing data is inserting documents and retrieving their unique identifiers, or IDs. This tutorial will guide you through the process of inserting a document into a MongoDB collection using PyMongo and how to retrieve the document’s ID. We will start with basic operations and gradually move to more advanced use cases.

Getting Started with PyMongo

Before diving into the operations, ensure you have MongoDB installed and running on your machine. Next, install PyMongo using pip:

pip install pymongo

After installation, establish a connection to your local MongoDB server:

from pymongo import MongoClient

client = MongoClient('localhost', 27017)
db = client['mydatabase']
collection = db['mycollection']

This code snippet connects to a local MongoDB server, selects a database named ‘mydatabase’, and specifies a collection within that database.

Inserting a Document and Retrieving its ID

The basic operation to insert a document into a MongoDB collection involves the insert_one method. When a document is inserted, MongoDB automatically assigns a unique identifier, known as _id. Here is how you can insert a document and retrieve its ID:

document = {'name': 'John Doe', 'age': 30, 'occupation': 'Engineer'}
insert_result = collection.insert_one(document)
document_id = insert_result.inserted_id
print('Inserted document ID:', document_id)

This will output the unique identifier of the inserted document. Notice that the _id field is automatically generated by MongoDB if it’s not explicitly defined in the document.

Working With Custom IDs

If you prefer to define your own custom IDs instead of relying on MongoDB’s auto-generated ones, you can do so by specifying an _id field in your document. Here’s an example:

custom_document = {'_id': 'custom_id_123', 'name': 'Jane Doe', 'age': 28, 'occupation': 'Doctor'}
try:
    insert_result = collection.insert_one(custom_document)
    print('Inserted custom ID document:', insert_result.inserted_id)
except pymongo.errors.DuplicateKeyError:
    print('Document with the specified custom ID already exists.')

This code attempts to insert a document with a custom _id. If a document with the same _id already exists in the collection, a DuplicateKeyError will be thrown.

Bulk Insertion and IDs Retrieval

PyMongo also allows for the insertion of multiple documents at once using the insert_many method. This can be particularly useful when you need to insert a large number of documents. Here’s how to perform a bulk insertion:

documents = [
  {'name': 'Alice', 'age': 24, 'occupation': 'Artist'},
  {'name': 'Bob', 'age': 35, 'occupation': 'Writer'},
  {'name': 'Charlie', 'age': 29, 'occupation': 'Musician'}
]

insert_result = collection.insert_many(documents)
document_ids = insert_result.inserted_ids
print('Inserted documents IDs:', document_ids)

This will output a list of unique identifiers for each inserted document. Notice how simple it is to insert multiple documents and retrieve their IDs.

Advanced: Working with Embedded Documents

In some cases, your documents may contain embedded documents or arrays. Inserting these documents and retrieving their IDs works in the same manner. Here is an example of inserting a document with an embedded document:

complex_document = {
  'name': 'Daniel',
  'age': 42,
  'education': {'degree': 'PhD', 'field': 'Computer Science'}
}

insert_result = collection.insert_one(complex_document)
print('Inserted document with embedded document ID:', insert_result.inserted_id)

This example demonstrates the flexibility of MongoDB and PyMongo in handling complex data structures.

Conclusion

PyMongo provides a straightforward and efficient way to work with MongoDB from Python. Whether you’re inserting a single document, multiple documents, or dealing with complex data structures, retrieving the ID of inserted documents is a simple process. This feature is fundamental for tracking entries and ensuring data integrity in your applications.

Next Article: PyMongo bulk insert: How to insert a list of documents/dicts

Previous Article: PyMongo: How to list all collections in a database

Series: Data Persistence in Python – Tutorials & Examples

Python

You May Also Like

  • Python Warning: Secure coding is not enabled for restorable state
  • Python TypeError: write() argument must be str, not bytes
  • 4 ways to install Python modules on Windows without admin rights
  • Python TypeError: object of type ‘NoneType’ has no len()
  • Python: How to access command-line arguments (3 approaches)
  • Understanding ‘Never’ type in Python 3.11+ (5 examples)
  • Python: 3 Ways to Retrieve City/Country from IP Address
  • Using Type Aliases in Python: A Practical Guide (with Examples)
  • Python: Defining distinct types using NewType class
  • Using Optional Type in Python (explained with examples)
  • Python: How to Override Methods in Classes
  • Python: Define Generic Types for Lists of Nested Dictionaries
  • Python: Defining type for a list that can contain both numbers and strings
  • Using TypeGuard in Python (Python 3.10+)
  • Python: Using ‘NoReturn’ type with functions
  • Type Casting in Python: The Ultimate Guide (with Examples)
  • Python: Using type hints with class methods and properties
  • Python: Typing a function with default parameters
  • Python: Typing a function that can return multiple types