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)

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.0.tar.gz (11.6 kB view hashes)

Uploaded Source

Supported by

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