4. CRUD classmethods
4.1. Operations
The following class methods are supported by motormongo’s Document class:
All examples below assume User is a subclass of motormongo provided Document class.
4.2. Create
4.2.1. insert_one(document: dict, **kwargs) -> Document
Inserts a single document into the database.
user = await User.insert_one({
"name": "John",
"age": 24,
"alive": True
})
Alternatively, using **kwargs:
user = await User.insert_one(
name="John",
age=24,
alive=True)
And similarly, with a dictionary:
user_document = {
"name": "John",
"age": 24,
"alive": True
}
user = await User.insert_one(**user_document)
4.2.2. insert_many(List[document]) -> tuple[List[‘Document’], Any]
users, user_ids = await User.insert_many(
[
{
"name": "John",
"age": 24,
"alive": True
},
{
"name": "Mary",
"age": 2,
"alive": False
}
]
)
or
docs_to_insert = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]
inserted_docs, inserted_ids = await User.insert_many(docs_to_insert)
4.3. Read
4.3.1. find_one(query, **kwargs) -> Document
user = await User.find_one(
{
"_id": "655fc281c440f677fa1e117e"
}
)
Alternatively, using **kwargs:
user = await User.find_one(_id="655fc281c440f677fa1e117e")
Note: The _id string datatype here is automatically converted to a BSON ObjectID, however, motormongo handles
the
scenario when a
BSON ObjectId is passed as the _id datatype:
from bson import ObjectId
user = await User.find_one(
{
"_id": ObjectId("655fc281c440f677fa1e117e")
}
)
4.3.2. find_many(query, limit, **kwargs) -> List[Document]
users = await User.find_many(age={"$gt": 40}, alive=False, limit=20)
or
query = {"age": {"$gt": 40}, "alive": False}
users = await User.find_many(**query, limit=20)
4.4. Update
4.4.1. update_one(query, updated_fields) -> Document
updated_user = await User.update_one(
{
"_id": "655fc281c440f677fa1e117e"
},
{
"name": "new_name",
"age": 30
}
)
or
query_criteria = {"name": "old_name"}
update_data = {"name": "updated_name"}
updated_user = await User.update_one(query_criteria, update_data)
4.4.2. update_many(qeury, fields) -> Tuple[List[Any], int]
updated_users, modified_count = await User.update_many({'age': {'$gt': 40}}, {'category': 'senior'})
another example:
updated_users, modified_count = await User.update_many({'name': 'John Doe'}, {'$inc': {'age': 1}})
4.5. Delete
4.5.1. delete_one(query, **kwargs) -> bool
deleted = await User.delete_one({'_id': '507f191e810c19729de860ea'})
Alternatively, using **kwargs:
deleted = await User.delete_one(name='John Doe')
4.5.2. delete_many(query, **kwargs) -> int
deleted_count = await User.delete_many({'age': {'$gt': 40}})
Another example:
# Delete all users with a specific status
deleted_count = await User.delete_many({'status': 'inactive'})
Alternatively, using **kwargs:
deleted_count = await User.delete_many(status='inactive')
4.6. Mixed
4.6.1. find_one_or_create(query, defaults) -> Tuple[‘Document’, bool]
user, created = await User.find_one_or_create({'username': 'johndoe'}, defaults={'age': 30})
4.6.2. find_one_and_replace(query, replacement) -> Document
replaced_user = await User.find_one_and_replace({'username': 'johndoe'}, {'username': 'johndoe', 'age': 35})
4.6.3. find_one_and_delete(query) -> Document
deleted_user = await User.find_one_and_delete({'username': 'johndoe'})
4.6.4. find_one_and_update_empty_fields(query, update_fields) -> Tuple[‘Document’, bool]
updated_user, updated = await User.find_one_and_update_empty_fields(
{'username': 'johndoe'},
{'email': 'johndoe@example.com', 'age': 30}
)