Skip to main content

Easy to use wrapper around pymongo for easy access to MongoDB.

Project description

Mongeasy

Mongeasy is a easy to use library to be used for simple access to a MongoDB database, without any need for schemas or validation. Just store the data as it is used in your application.

Connection

Connection to the database is handled automtically for you if you have the conenction information in a configfile or set as environment variables.

Connection using configfile

Create a file called mongeasy.conf and place it in your project root folder.

The contents of the file should be:

[mongoeasy]
connection_string = mongodb://localhost:27017/
database_name = mydatabase

Connection using environment variables

You can, as an alternative method, define your connection information using environment variables. Just set these two:

MONGOEASY_CONNECTION_STRING=mongodb://localhost:27017/
MONGOEASY_DATABASE_NAME=mydatabase

Create a document class

To use Mongeasy you will create a document class that can be used with the collection of choice. To do this you will use the create_document_class factory function like this:

from mongeasy import create_document_class


User = create_document_class('User', 'users')

The first argument is the name the class will get and the second argument is the name of the collection to use. If the collection does not exist it will be created when you use the class to store documents.

You will not need to assign the returned value to a class variable as in the example above, as the generated class is injected into the current namespace:

from mongeasy import create_document_class


create_document_class('User', 'users')

# The class User exist from this point in the code

Create a store a document

You can create a documnet by using the generated class. You can either use keyword arguments or pass a dict.

from mongeasy import create_document_class


User = create_document_class('User', 'users')

# Create a document using keyword arguments
user1 = User(name='Alice', age=25)
user1.save()

# Create a document using a dict
user2 = User({'name': 'Bob', 'age': 30})
user2.save()

Find documents

You can find documents using the find method on the generated class. This method will return a list of documents.

from mongeasy import create_document_class


User = create_document_class('User', 'users')

# Find all documents
users = User.all()

# Find all documents with age 25
users = User.find({'age': 25})

Find one document

You can find one document using the find method on the generated class.

Find will return a ResultList object that can be used to get the first, or last, document in the list. If no document is found None is returned.

from mongeasy import create_document_class


User = create_document_class('User', 'users')

# Find one document with age 25
user = User.find({'age': 25}).first()

if user is None:
    print('No user found')

Update a document

You can update a document just by changing the attributes and then calling the save method.

from mongeasy import create_document_class


User = create_document_class('User', 'users')

# Find one document with age 25
user = User.find({'age': 25}).first()

if user is None:
    print('No user found')
else:
    # Update the age of the user
    user.age = 26
    user.save()

Delete a document

You can delete a document by calling the delete method on the document.

from mongeasy import create_document_class


User = create_document_class('User', 'users')

# Find one document with age 25
user = User.find({'age': 25}).first()

if user is None:
    print('No user found')
else:
    # Delete the user
    user.delete()

You can also delete all documents in a collection by calling the delete method on the generated class.

from mongeasy import create_document_class


User = create_document_class('User', 'users')


# Delete using a filter
User.delete({'age': 25})

# Delete all documents in the collection
User.delete()

Indexes

You can create indexes on the collection by using the create_index method on the generated class.

from mongeasy import create_document_class


User = create_document_class('User', 'users')

# Create a unique index on the name field
User.create_index('name', unique=True)

Other uses

When you create the document class you have an option to pass additional bases classes. You can use this feature to add functionality to the generated class.

This can also be useful if you want to use Mongeasy with for example flask-login.

from flask import Flask
from flask_login import UserMixin, LoginManager
from mongeasy import create_document_class
from bson import ObjectId

login_manager = LoginManager()
# Create User class with mongeasy and UserMixin from flask_login as a base class
User = create_document_class('User', 'users', base_classes=(UserMixin,))
def get_id(self):
    return str(self._id)
# Add get_id method to User class
User.get_id = get_id


def create_app():
    app = Flask(__name__)
   # Define the user loader function for Flask-Login
    @login_manager.user_loader
    def load_user(user_id):
        # Load the user object from the database using the user_id
        user_id = ObjectId(user_id)
        user = User.find(_id=user_id).first()
        return user

    return app

ResultList

All queries that can return more than one document will return a ResultList object. This object can be used to get the first or last document in the list, or None if no document is found.

from mongeasy import create_document_class


User = create_document_class('User', 'users')

# Find one document with age 25
user = User.find({'age': 25}).first()

if user is None:
    print('No user found')

There are also other methods on the ResultList object that can be used. These are:

  • first - Get the first document in the list or None if no document is found
  • last - Get the last document in the list or None if no document is found
  • first_or_none - Get the first document in the list or None if no document is found, same as first
  • last_or_none - Get the last document in the list or None if no document is found, same as last
  • map - Apply a given function to each element in the list and return a new ResultList containing the results
  • filter - Filter the list using a given function and return a new ResultList containing the results
  • reduce - Apply a given function to each element in the list and return a single value
  • group_by - Group the list by a given key and return a dict with the results grouped by the key
  • random - Get a random document from the list or None if no document is found

Contributing

Contributions are welcome. Please create a pull request with your changes.

Issues

If you find any issues please create an issue on the github page.

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

Mongeasy-0.1.5.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Mongeasy-0.1.5-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file Mongeasy-0.1.5.tar.gz.

File metadata

  • Download URL: Mongeasy-0.1.5.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for Mongeasy-0.1.5.tar.gz
Algorithm Hash digest
SHA256 2afb93e782932e23dd687ad0c8d4fc3b5d804f70574c9ff1c5ac4b4474b094c3
MD5 7b23a2d6ce89de65287c216bcbd7198a
BLAKE2b-256 1d93f86eb99328666c64ddbf8b68198e38831ca659e25cb9814f080c0e48637a

See more details on using hashes here.

File details

Details for the file Mongeasy-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: Mongeasy-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for Mongeasy-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8d7c2a97008795da41bd68b3e582cc48fb5769a0b003434d0bf7c292534524bc
MD5 61f74b782146e6cd631e068a83869379
BLAKE2b-256 166f485c529803fb790eb7277d363efa2e4278c7bc3f9f3e7f25f5d08512045c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page