Using Motorturbine is meant to be a streamlined experience, creating an environment where it's the only needed connection to interact with any object in a database.
At first we need to establish a connection to the database. Using ~motorturbine.connection.Connection
's ~motorturbine.connection.Connection.connect
all future operations will be made by utilising that connection.
Connection.connect(host='localhost', port=27017)
The next step after a global connection is established is to model your documents using the ~motorturbine.document.BaseDocument
class. Modeling is achieved by populating the documents attributes using the supplied Fields <motorturbine.fields.BaseField>
from motorturbine import BaseDocument, fields
def class Person(BaseDocument):
name = fields.StringField(default='Nobody')
age = fields.IntField(required=True)
From here on out each document object can be considered like a typed object.
person1 = Person(name="Steve", age=25)
person2 = Person(age=44)
person2.age = 60
When all transformations are done objects can be inserted into the database by calling ~motorturbine.document.BaseDocument.save
.
Note
~motorturbine.document.BaseDocument.save
is a coroutine function and therefore requires awaiting.
async def save_person(person):
await person.save()
The created collections (or document classes) can be queried by using one of the classmethodds ~motorturbine.document.BaseDocument.get_object
or ~motorturbine.document.BaseDocument.get_objects
. These methods will search the collection that is automatically created when inserting a new document. To specify the parameters it is possible to use one or multiple instances of ~motorturbine.queryset.QueryOperator
.
async def get_sixty_plus():
oldies = await Person.get_objects(age=Gte(60))
return oldies
In this example motorturbine.queryset.Gte
is used to look for all entries with Person.age >= 60.
Once everything is set up, instead of just setting values directly there is a fancier way to update your fields by utilising mongos inbuilt atomic update capabilities.
Values that are updated this way don't need to match their old state since they just add to the state instead of completely changing it.
async def happy_birthday(person):
person.age = Inc(1)
await person.save()
In this example the motorturbine.updateset.Inc
operator is used to increase the persons age by one year. For more information about updating see ~motorturbine.updateset.UpdateOperator
.