Saving instances¶
Creating new instances of a document¶
The easiest way of creating a new instance of a document is using Document.objects.create. Alternatively, you can create a new instance and then call save on it.
Updating instances¶
To update an instance, just make the needed changes to an instance and then call save.
Deleting instances¶
Deleting an instance can be easily accomplished by just calling delete on it:
Sometimes, though, the requirements are to remove a few documents (or all of them) at a time. MotorEngine also supports deleting using filters in the document queryset.
Bulk inserting instances¶
AIOMotorEngine supports bulk insertion of documents by calling the bulk_insert method of a queryset with an array of documents:
async def create_users():
users = [
User(name="Bernardo"),
User(name="Heynemann")
]
users = await User.objects.bulk_insert(users)
assert len(users) == 2
assert users[0]._id
assert users[1]._id
io_loop.run_until_complete(create_users())