Install Mongita via pip
masterInstall the Mongita library using pip to use it as an embedded document database in your Python applications.
pip3 install mongitarepository·master·Indexed 21 days ago
https://github.com/scottrogowski/mongitaA 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.
Install the Mongita library using pip to use it as an embedded document database in your Python applications.
pip3 install mongitaThis 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'})The mongita.Cursor object is returned by find() operations and allows you to iterate through results and apply modifiers.
Supported Methods:
sort(key_or_list, direction=None)next()limit(limit)skip(skip)clone()close()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')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()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:
InvalidOperationOperationFailureDuplicateKeyErrorMongitaNotImplementedError (raised when using unsupported features)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)