A simple way to create documents in MongoDB
Project description
mongoday
mongoday
is a Python library that simplifies document creation in MongoDB. It does not depend on specific library as
it is agnostic but built using pymongo
library. This README provides a brief overview of how to use mongoday
to work
with documents in MongoDB.
Its development is undergoing but it provides a good starting point for creating documents in MongoDB.
Installation
You can install mongoday
using pip:
pip install mongoday
Getting Started
To get started with mongoday
, you'll need to create a model for your MongoDB documents. Here's an example of how to
create a simple model using mongoday
:
from mongoday.field import BaseField, MongoIDField, StringField
from mongoday.model import BaseModel, IndexStore
from pymongo import MongoClient
class User(BaseModel):
def __init__(self, collection):
self.username = StringField()
super(User, self).__init__(collection)
# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["mydatabase"]
collection = db["users"]
# Setup the store (mentioned below) for tracking indexes
index_store = IndexStore(collection=db["index_store"])
# Initialize a User document
user = User(collection)
user.username = "John"
user.save()
# Retrieve a User document
retrieved_user = User(collection).get(user.id)
print("Username:", retrieved_user.username)
Indexes
# Before using indexes, make sure to setup store index which tracks the indexes and recreates automatically if changed
index_store = IndexStore(collection=db["index_store"])
To specify indexes, add an indexes attribute to your model class. Here's an example:
class User(BaseModel):
indexes = [
[('username', 1)], # Create an ascending index on the 'username' field
[('created', -1)] # Create a descending index on the 'created' field
]
Or could be specified in the constructor, this way it won't override the default indexes specified by the BaseModel class but will be added to them.
class User(BaseModel):
def __init__(self, collection):
.....
# Specify indexes
indexes = [
[('username', 1)], # Create an ascending index on the 'username' field
[('created', -1)] # Create a descending index on the 'created' field
]
self.add_indexes(indexes)
Contribution
Feel free to contribute to this project by opening issues or submitting pull requests on GitHub.
License This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mongoday-0.0.4.tar.gz
.
File metadata
- Download URL: mongoday-0.0.4.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e19be31e3abed8022ee4aec8b8ea8dbc8e7f1af2196496b8d811188ff835a64e |
|
MD5 | 944a76cc82f72c66ec0377ce943baef7 |
|
BLAKE2b-256 | 6c3128688d14be5b9bf24a30bb0c6c52a7258903806cf23cf2416116e57ed86e |
Provenance
File details
Details for the file mongoday-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: mongoday-0.0.4-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32efb7c2ff18d6f85ac32bf899318480c6e823ff563c119a87801d9011cb6d37 |
|
MD5 | 7f14e2384b374a52c5899be40f6238dd |
|
BLAKE2b-256 | 50e758b2588189ec25136ab62a37597f7b8cde15d79724749f00500e23b35c73 |