A simple MongoDB python driver
- Easy MongoDB Queries: Simplify querying MongoDB by providing an intuitive and straightforward interface, making it as simple as working with Firestore.
- Streamlined Updates and Deletions: Perform updates and deletions on MongoDB collections with ease, reducing the complexity of modifying data.
pip3 install mongofireYou need first to initialize the client in your main.py file.
from mongofire import MongoFire
MongoFire.initialize('mongodb://localhost:27017/')And then you can use it on any file in your project like this
from mongofire import MongoDB
db = MongoDB('myAppDatabase')To create or overwrite a single document, use the following example:
data = {
'name': 'Mohamed',
'age': 20,
}
db.collection('users').document('my_custom_uid').set(data)If the document does not exist, it will be created. If the document does exist, its contents will be overwritten with the newly provided data, unless you specify that the data should be merged into the existing document, as follows:
doc_ref = db.collection('users').document('my_custom_uid')
doc_ref.set({'gender': 'male'}, merge=True)If you want to update any field in the document, you can use the update method. You can increment values, push or pull items from the list, rename fields, and more.
from mongofire import Field, FieldValue
db.collection('users').document('my_uid').update({
'name': Field.rename('username'),
'age': FieldValue.increment(1),
})Sometimes the document id doesn't make sense and it's ok to be random in that case you can use the add method instead of the set method.
doc_ref = db.collection('users').add({
'name': 'Max',
'age': 30,
})You can get the randomly generated id by doc_ref.id.
Delete any document as follows:
db.collection('users').document('doc_id').delete()If you want to know how to delete or update multiple documents or delve into the lib, read the full documentation at mongofire.readthedocs.io.
We welcome contributions from the community. To contribute to the library, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes, including tests and documentation.
- Run the test suite to ensure everything is working correctly.
- Submit a pull request.
The library is licensed under the MIT License. See the LICENSE file for more details.
- Firestore: Firestore provided inspiration with its simplicity and productivity. Although not directly used as a dependency, it influenced the development of this library.
- MongoDB: MongoDB served as the solution to build something similar to Firestore that is free and open source for all.
If you have any questions, issues, or suggestions, please open an issue on the GitHub repository or contact the maintainer at pshteam.developers@gmail.com.
- v1.0.0 (2023-05-25): first release.
- Sub Collection: add sub collcetions for documents.