-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for ObjectId in to_builtins/convert #516
Comments
Seems to work fine for me: In [1]: import bson
In [2]: import msgspec
In [3]: class Ex(msgspec.Struct):
...: _id: bson.objectid.ObjectId
...: x: int
...: y: str
...:
In [4]: msg = {"_id": bson.objectid.ObjectId(), "x": 1, "y": "two"}
In [5]: msg
Out[5]: {'_id': ObjectId('64da70341916b9d3c85d516d'), 'x': 1, 'y': 'two'}
In [6]: type(msg["_id"])
Out[6]: bson.objectid.ObjectId
In [7]: msgspec.convert(msg, type=Ex)
Out[7]: Ex(_id=ObjectId('64da70341916b9d3c85d516d'), x=1, y='two') Can you write out an example struct definition and dict input showing the issue? Something that doesn't require instantiating a mongo client that I can run locally. |
Hmm. Actually I did not try to do it. I was under impression that in Struct I can use only types described at https://jcristharif.com/msgspec/supported-types.html |
There's a line at the bottom of the list saying
If you have concrete suggestions for how we can make this clearer, please let me know (or better yet, submit a PR :)). When using You're right though, currently there's no way to get
If this is insufficient, we can add a way to support |
Thank you :) |
Description
Values of ObjectId type are used as primary key in every collection in MongoDB database.
From the MongoDB documentation:
At the moment it is not possible to load object from mongodb (as python dict) and convert it into Struct instance because there is no support for ObjectId type.
Just a simple example which illustrates how a record created in mongodb collection has an ObjectId field.
ObjectID documentation:
The text was updated successfully, but these errors were encountered: