Random Document Generator with PyMongoEngine

  • Share this:

Code introduction


This function uses PyMongoEngine to create a random document and inserts it into the specified collection. Depending on the document type (Person, Product, Book) passed in, the function will create a document of the corresponding type and fill it with random data.


Technology Stack : PyMongoEngine

Code Type : Function

Code Difficulty : Intermediate


                
                    
def create_random_document(collection, document_type):
    """
    Generates a random document with specified type and inserts it into the given collection.
    """
    import random
    from mongoengine import Document, StringField, IntField, FloatField, ListField, EmbeddedDocument, EmbeddedDocumentField
    
    # Define the embedded document structure
    class Address(EmbeddedDocument):
        street = StringField()
        city = StringField()
        zip_code = IntField()
    
    # Define the document structure based on the document_type
    if document_type == 'Person':
        class Person(Document):
            name = StringField()
            age = IntField()
            email = StringField()
            address = EmbeddedDocumentField(Address)
    
    elif document_type == 'Product':
        class Product(Document):
            name = StringField()
            price = FloatField()
            categories = ListField(StringField())
    
    elif document_type == 'Book':
        class Book(Document):
            title = StringField()
            author = StringField()
            published_year = IntField()
            pages = IntField()
    
    # Create a random document
    random_document = document_type()
    random_document.name = f"Name_{random.randint(1, 100)}"
    random_document.age = random.randint(18, 70) if document_type == 'Person' else None
    random_document.email = f"email_{random.randint(1, 1000)}@example.com" if document_type == 'Person' else None
    random_document.street = f"Street_{random.randint(1, 1000)}" if document_type == 'Person' else None
    random_document.city = f"City_{random.randint(1, 1000)}" if document_type == 'Person' else None
    random_document.zip_code = random.randint(10000, 99999) if document_type == 'Person' else None
    random_document.price = round(random.uniform(10.0, 1000.0), 2) if document_type == 'Product' else None
    random_document.categories = [f"Category_{random.randint(1, 5)}"] * random.randint(1, 3) if document_type == 'Product' else None
    random_document.title = f"Title_{random.randint(1, 1000)}" if document_type == 'Book' else None
    random_document.author = f"Author_{random.randint(1, 1000)}" if document_type == 'Book' else None
    random_document.published_year = random.randint(1900, 2023) if document_type == 'Book' else None
    random_document.pages = random.randint(100, 1000) if document_type == 'Book' else None
    
    # Insert the document into the collection
    random_document.save(collection)