Basic Usage

Create a new entity

Suppose two characters: “Ramza”, and “Alma”, are to created. The ORM provides two ways to create a new entity.

Using the constructor directly

ramza = Character('Ramza')
alma  = Character('Alma')

character_collection.post(ramza)
character_collection.post(alma)

Using the “new” method

session = entity_manager.open_session(supervised=False)
collection = session.collection(Character)

ramza = collection.new(name = 'Ramza')
alma = collection.new(name = 'Alma')

collection.post(ramza)
collection.post(alma)

Note

for the following example, assume that ramza.id is 1 and alma.id is 2.

List, query or filter entities

To list all characters (documents),

characters = collection.filter()

for character in characters:
    print('{}: {}'.format(character.id, character.name))

Then, you should see:

1: Ramza
2: Alma

Now, to find “Ramza”,

characters = collection.filter({'name': 'Ramza'})

for character in characters:
    print('{}: {}'.format(character.id, character.name))

Then, you should only see:

1: Ramza

Note

The criteria (e.g., in this case {'name': 'Ramza'}) is the same one used by pymongo.collection.Collection.

Retrieve an entity by ID

Now, to retrieve an entity by ID,

alma = collection.get(2)

Note

There is no auto-conversion from any given ID to bson.ObjectId as the ID can be anything. If the ID of the target entity is of type bson.ObjectId, e.g., "2" is a string representation of the ObjectId, the code has to be alma = collection.get(bson.ObjectId('2')). (Assume that instantiating is okay.)

Update entities

Let’s say you want to rename “Alma” to “Luso”.

alma = collection.get(2)

alma.name = 'Luso'

You can update this by

collection.put(character)

Delete entities

collection.delete(alma)
Read the Docs v: v2.1
Versions
latest
v2.1
v2.0
Downloads
PDF
HTML
Epub
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.