Skip to content

Commit e850871

Browse files
committed
Fix #208: add migrations information for MongoDB users
1 parent 3fab5e5 commit e850871

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

docs/migration/08_to_10.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ class Model(BaseModel):
3232
user_id: UUID4
3333
```
3434

35+
#### MongoDB
36+
37+
To avoid any issues, it's recommended to use the `standard` UUID representation when instantiating the MongoDB client:
38+
39+
```py
40+
DATABASE_URL = "mongodb://localhost:27017"
41+
client = motor.motor_asyncio.AsyncIOMotorClient(
42+
DATABASE_URL, uuidRepresentation="standard"
43+
)
44+
```
45+
46+
This parameter controls how the UUID values will be encoded in the database. By default, it's set to `pythonLegacy` but new applications should consider setting this to `standard` for cross language compatibility. [Read more about this](https://pymongo.readthedocs.io/en/stable/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient).
47+
48+
3549
### In database
3650

3751
Id. were before stored as strings in the database. You should make a migration to convert string data to UUID data.
@@ -72,6 +86,8 @@ ALTER TABLE "user" MODIFY id CHAR(36);
7286

7387
#### MongoDB
7488

89+
##### Mongo shell
90+
7591
For MongoDB, we can use a `forEach` iterator to convert the id. for each document:
7692

7793
```js
@@ -81,6 +97,28 @@ db.getCollection('users').find().forEach(function(user) {
8197
});
8298
```
8399

100+
##### Python
101+
102+
```py
103+
import uuid
104+
105+
import motor.motor_asyncio
106+
107+
108+
async def migrate_uuid():
109+
client = motor.motor_asyncio.AsyncIOMotorClient(
110+
DATABASE_URL, uuidRepresentation="standard"
111+
)
112+
db = client["database_name"]
113+
users = db["users"]
114+
115+
async for user in users.find({}):
116+
await users.update_one(
117+
{"_id": user["_id"]},
118+
{"$set": {"id": uuid.UUID(user["id"])}},
119+
)
120+
```
121+
84122
## Splitted routers
85123

86124
You now have the responsibility to **wire the routers**. FastAPI Users doesn't give a bloated users router anymore.

0 commit comments

Comments
 (0)