BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
1.94k stars 203 forks source link

Support `Binary.createFromBase64` Type #808

Closed tillwf closed 7 months ago

tillwf commented 7 months ago

Discussed in https://github.com/roman-right/beanie/discussions/800

Originally posted by **tillwf** December 8, 2023 Hello, I have the kind of document: ```bson { _id: ObjectId('656f001650078dc1d50872ed'), created_at: ISODate('2023-12-05T10:48:54.641Z'), user_id: Binary.createFromBase64('MjkyMmZkYmUtOTM1Yi0xMWVlLTlkMjEtN2U2NjQwYmEyNGEw', 4), } ``` and I want to make queries on this collection. So I created this file: ```python import base64 import uuid from datetime import datetime from typing import Optional from beanie import Document, init_beanie from beanie import BsonBinary from pydantic import BaseModel, Field from bson import Binary, ObjectId from pymongo import MongoClient # HomeTracker document class class HomeTrackers(Document): id: ObjectId = Field(default_factory=ObjectId, alias="_id") created_at: datetime user_id: BsonBinary class Settings: name = "collection_name" class Config: arbitrary_types_allowed = True ``` but when I make a query I get this error: ``` bson.errors.InvalidBSON: invalid length or type code ``` and if I remove the `user_id` field, everything works. Is the type `Binary.createFromBase64(..., 4)` supported by beanie?
tillwf commented 7 months ago

The problem seems to be in the encoding itself: https://stackoverflow.com/questions/77680833/load-mongo-binary-createfrombase64-field-in-python