Skip to content

Latest commit

 

History

History
195 lines (135 loc) · 8.04 KB

README.md

File metadata and controls

195 lines (135 loc) · 8.04 KB

Mongolia

Mongolia is a wrapper of the pymongo library that lets you work with your mongo objects in a much more pythonic way.

It can be installed by pip:

pip install mongolia

The README that follows is a tutorial for the library. See the doc strings on the classes and functions themselves for the complete documentation.

For inquiries and customizations, email info[at]zagaran.com

Connecting to Your Database

Connecting to mongolia is simple:

from mongolia import connect_to_database, authenticate_connection
connect_to_database(host="example.com", port="12345")
authenticate_connection("username", "password")

# If your user is only authorized to access a single database, instead use this
authenticate_connection("username", "password", db="somedb")

Any further calls to mongolia will use this connection. If you are using mongolia with flask, this connection setup should go in app.py or in the global scope of a file imported by app.py. If you are using monglia with django, this connection setup should go in settings.py.

If you need to add a user to mongo, mongolia provides that functionality as well:

from mongolia import add_user
add_user("username", "password")

Note that in order to add the first user to a database, you need to be running mongo in unauthenticated mode. BE VERY CAREFUL WITH THIS. Make sure to shut down mongo and start in in authenticated mode when your done or else ANYONE IN THE WORLD WILL BE ABLE TO STEAL YOUR DATA.

DatabaseObject

By extending the python dictionary class, a DatabaseObject lets you interact easily with items in your databases. These are simply dictionaries with a few extra methods:

from mongolia import DatabaseObject, ID_KEY
path = "somedb.somecoll"

# Put a new item in the database
item = DatabaseObject.create({ID_KEY: "stuff", "key": "value"}, db=path)

# Modify the item and re-save it
item["key"] = "newval"
item.save()

# Get an item out of the database
item = DatabaseObject("stuff", db=path)
    # or just
item = DatabaseObject("stuff", path)

# Load an item by attribute rather than _id
item = DatabaseObject({"key": "newval"}, db=path)
    # or just
item = DatabaseObject(db=path, key="newval")

# Delete an item by
item.remove()

# Rename an item (changing it's ID_KEY)
item.rename("different_stuff")

It is worth noting that the ID_KEY of a dictionary (defined by Mongo as "_id") is always a required key of the DatabaseObject (so it will be an error if you try to create a DatabaseObject without either giving it an ID_KEY entry or setting random_id=True). When updating a DatabaseObject, changing the ID_KEY is also an error (as this is used by mongo to enforce uniqueness); instead, use the rename method.

DatabaseCollection

DatabaseCollection returns a collection as an explicit list of DatabaseObjects. When using this on larger collections, you can run out of memory; see "Dealing with Large Collections" below. Standard usage looks like this:

from mongolia import DatabaseCollection
path = "somedb.somecoll"

# Returns the entire collection
coll = DatabaseCollection(path)

# Returns the first ten elements of the collection (pages are 1-indexed for UI reasons)
coll = DatabaseCollection(path, page=1, page_size=10)

# Returns the number of elements in the collection
count = DatabaseCollection.count(path)

# Returns a specific filtering of the collection
coll = DatabaseCollection(path, query={"key": "newval"})
  # or just
coll = DatabaseCollection(path, key="newval")

# Returns a read-only copy of the collection
# (the items in the list will just be dicts, not DatabaseObjects, so you can't save them)
coll = DatabaseCollection(path, readonly=True)

# Returns a list of a specific field of elements in the