-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomic_updates.py
More file actions
45 lines (36 loc) · 1.36 KB
/
Copy pathatomic_updates.py
File metadata and controls
45 lines (36 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import asyncio
from datetime import datetime
import mongojet
import msgspec
import mongospec
from mongospec import MongoDocument
class User(MongoDocument):
__collection_name__ = "users"
name: str
email: str
version: int = 0
created_at: datetime = msgspec.field(default_factory=datetime.now)
async def main():
mongo_client = await mongojet.create_client("mongodb://localhost:27017")
await mongospec.init(mongo_client.get_database("example_db"), document_types=[User])
try:
# Insert a user with initial version 0
user = User(name="Alice", email="alice@example.com", version=0)
await user.insert()
user_id = user._id
current_version = 5 # intentionally incorrect version for demonstration
new_profile = {"newsletter": False}
# Attempt to update the user with a mismatched version (expected to fail)
updated_user = await User.find_one_and_update(
{"_id": user_id, "version": current_version}, # Only update if version matches (which it won't)
{
"$set": {"profile": new_profile},
"$inc": {"version": 1}
},
return_updated=True
)
if not updated_user:
print("Update failed - document was modified by another process")
finally:
await mongospec.close()
asyncio.run(main())