yamongo 0.0.3
Easy to use access to MongoDb databases for basic operations
Requires Python: >=3.6.2
Keywords: MongoDB,Database,pymongo

# YaMongo
Easy to use package for MongoDB access in Python

This is a work in progress. 

The idea is to create a Python Package that will let users access a MongoDB database and perform the most common tasks as easy and simple as possible.

## The Concept
The concept is easy. The user of this package will need to create a class per collection. The collection class is created by a call to the create_collection_class function.
Before use, an init_db function must be called that will set up the database connection. 

The basic use case is like this:

```python
from yamongo import init_db, create_collection_class

init_db('mongodb://username:password@host:port')

# Create a user class
User = create_collection_class('User', 'users')

user = User(
    **{
        'first_name': 'Alice',
        'last_name': 'Smith',
        'email': 'alice@email.com'
    })

user.save()

user = User.find(first_name='Alice').first_or_none()
if user:
    user.first_name = 'Bob'
    user.save()
```    

## Documentation
The concept of this library is that we represent documents in a collection by classes that are generated by a factory function, init_db.

We can then perform operations, either on the document (the instance object), or the collection (class level methods)

### Database Initialization
To initialize the database connection we need to call the init_db function.

**init_db**
```python
init_db(connection_str: str, database: str) -> None
```
connection_str: The database connection string to be used

database: Name of the database to be used

return: None

### Collection Class Creation
Create a collection class by using the factory function create_collection_class.

**create_collection_class**
create_collection_class(class_name: str, collection_name: str) -> Document
```python
User = create_collection_class('User', 'users')
```

collection_name: str or None, name of collection in database. If None, the class name will be used

return: The newly created collection class

## Document Operations
### Document Creation
To create a new document, all we need to do is to create an object from our collection class and save it.

We can use either a dictionary or keywords when creating the object:

```python
# Create and save a document using a dictionary
user = User(**{
    'first_name': 'Alice',
    'last_name': 'Anderson',
    'email': 'alice@email.com'
})

user.save()
```

Another option is to create the object using keyword arguments:
```python
# Create and save a document using keywords
user = User(first_name='Alice', last_name='Anderson', 'email='alice@email.com')

user.save()
```

## Save and Update a Document
Calling the save method on a document object will either create a new document in the database (if _id is None, that is a newly created object) or update an existing object if the objects _id is set.
```python
# Create and save a document using keywords
user = User(first_name='Alice', last_name='Anderson', 'email='alice@email.com')

# Saves a new document
user.save()

# Retrieve a document from the database
user = User.find(first_name='Alice').first_or_none()

# Check if a user is found
if user is not None:
    user.last_name = 'Allan'
    # As this object has an _id we will update the existing document
    user.save()
```