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.
-
QuerySet.
create
(**kwargs)¶ Creates and saved a new instance of the document.
def handle_user_created(user): try: assert user.name == "Bernardo" finally: io_loop.stop() def create_user(): User.objects.create(name="Bernardo", callback=handle_user_created) io_loop.add_timeout(1, create_user) io_loop.start()
-
Document.
save
(**kwargs)¶ Creates or updates the current instance of this document.
def handle_user_created(user): try: assert user.name == "Bernardo" finally: io_loop.stop() def create_user(): user = User(name="Bernardo") user.save(callback=handle_user_created) io_loop.add_timeout(1, create_user) io_loop.start()
Updating instances¶
To update an instance, just make the needed changes to an instance and then call save.
-
Document.
save
(**kwargs) Creates or updates the current instance of this document.
def handle_user_created(user): user.name = "Heynemann" user.save(callback=handle_user_updated) def handle_user_updated(user): try: assert user.name == "Heynemann" finally: io_loop.stop() def create_user(): user = User(name="Bernardo") user.save(callback=handle_user_created) io_loop.add_timeout(1, create_user) io_loop.start()
Deleting instances¶
Deleting an instance can be easily accomplished by just calling delete on it:
-
Document.
delete
(**kwargs)¶ Deletes the current instance of this Document.
def handle_user_created(user): user.delete(callback=handle_user_deleted) def handle_user_deleted(number_of_deleted_items): try: assert number_of_deleted_items == 1 finally: io_loop.stop() def create_user(): user = User(name="Bernardo") user.save(callback=handle_user_created) io_loop.add_timeout(1, create_user) io_loop.start()
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.
-
QuerySet.
delete
(**kwargs)¶ Removes all instances of this document that match the specified filters (if any).
def handle_user_created(user): User.objects.filter(name="Bernardo").delete(callback=handle_users_deleted) def handle_users_deleted(number_of_deleted_items): try: assert number_of_deleted_items == 1 finally: io_loop.stop() def create_user(): user = User(name="Bernardo") user.save(callback=handle_user_created) io_loop.add_timeout(1, create_user) io_loop.start()
Bulk inserting instances¶
MotorEngine supports bulk insertion of documents by calling the bulk_insert method of a queryset with an array of documents:
-
QuerySet.
bulk_insert
(documents, callback=None, alias=None)¶ Inserts all documents passed to this method in one go.
def handle_users_inserted(users):
try:
assert len(users) == 2
assert users[0]._id
assert users[1]._id
finally:
io_loop.stop()
def create_users():
users = [
User(name="Bernardo"),
User(name="Heynemann")
]
User.objects.bulk_insert(users, callback=handle_users_inserted)
io_loop.add_timeout(1, create_users)
io_loop.start()