Suppose two characters: “Ramza”, and “Alma”, are to created. The ORM provides two ways to create a new entity.
ramza = Character('Ramza')
alma = Character('Alma')
character_collection.post(ramza)
character_collection.post(alma)
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.
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.
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.)
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)
collection.delete(alma)