Mongita Documentation

repository·master·Indexed 21 days ago

https://github.com/scottrogowski/mongita

A lightweight, embedded document database for Python that implements a subset of the PyMongo/MongoDB interface. It supports both in-memory (MongitaClientMemory) and disk-based (MongitaClientDisk) storage, providing a self-contained library for unit testing or small embedded applications.

Tokens
1.1K
Snippets
3
Records
7
Agent score
26%

What's inside Mongita

  1. Quickstart: Basic CRUD operations with Mongita

    master

    This example demonstrates how to initialize a disk-based client, create a collection, and perform basic Create, Read, Update, and Delete (CRUD) operations using the Mongita API.

    from mongita import MongitaClientDisk
    
    # Initialize a disk-based client
    client = MongitaClientDisk()
    
    # Access a database and collection
    hello_world_db = client.hello_world_db
    mongoose_collection = hello_world_db.mongoose_collection
    
    # Create: Insert multiple documents
    mongoose_collection.insert_many([{'name': 'Meercat', 'does_not_eat': 'Snakes'},
                                     {'name': 'Yellow mongoose', 'eats': 'Termites'}])
    
    # Read: Count documents
    print(mongoose_collection.count_documents({}))
    
    # Update: Update a single document using $set
    mongoose_collection.update_one({'name': 'Meercat'}, {'$set': {"weight": 2}})
    
    # Read: Find documents with a query operator
    results = mongoose_collection.find({'weight': {'$gt': 1}})
    print(list(results))
    
    # Delete: Delete a single document
    mongoose_collection.delete_one({'name': 'Meercat'})
  2. Initialize Mongita clients (Memory vs Disk)

    master

    Mongita provides two main client types depending on your persistence needs. Both implement a subset of the PyMongo interface.

    • mongita.MongitaClientMemory: Stores documents in memory. Data is lost when the process ends.
    • mongita.MongitaClientDisk: Stores documents on disk. By default, data is stored in ~/.mongita. To specify a custom directory, pass the host parameter during initialization.

    Available client methods:

    • close()
    • list_database_names()
    • list_databases()
    • drop_database(name_or_database)
    # Using a custom directory for disk storage
    client = MongitaClientDisk(host='/path/to/your/db')
  3. Use Mongita Collection methods

    master

    The mongita.Collection object provides the primary interface for interacting with documents. It implements a subset of the PyMongo collection API.

    Core Methods:

    • insert_one(document)
    • insert_many(documents, ordered=True)
    • find_one(filter, sort)
    • find(filter, sort, limit)
    • replace_one(filter, replacement, upsert=False)
    • update_one(filter, update)
    • update_many(filter, update)
    • delete_one(filter)
    • delete_many(filter)
    • count_documents(filter)
    • distinct(key, filter)
    • create_index(keys)
    • drop_index(index_or_name)
    • index_information()
  4. Handle Mongita errors

    master

    Mongita uses a hierarchy of error classes for exception handling. All Mongita-specific errors inherit from mongita.errors.MongitaError (which is aliased as PyMongoError).

    Common Error Types:

    • InvalidOperation
    • OperationFailure
    • DuplicateKeyError
    • MongitaNotImplementedError (raised when using unsupported features)
  5. Supported Query and Update operators

    master

    Mongita currently implements a limited subset of MongoDB operators. Using unsupported operators may result in errors.

    Query Operators (Comparison):

    • $eq (equal)
    • $gt (greater than)
    • $gte (greater than or equal)
    • $lt (less than)
    • $lte (less than or equal)
    • $ne (not equal)
    • $in (in array)
    • $nin (not in array)

    Update Operators:

    • $set (sets the value of a field)
    • $inc (increments the value of a field)
    • $push (appends a value to an array)