Does anyone know of a Python library that can convert a class object to a mongodb BSON string? Currently my only solution is to convert the class object to JSON and then JSON to BSON.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This would be possible by converting the class instance to dictionary (as described in Python dictionary from an object's fields) and then using
bson.BSON.encodeon the resulting dict. Note that the value of__dict__will not contain methods, only attributes. Also note that there may be situations where this approach will not directly work.If you have classes that need to be stored in MongoDB, you might also want to consider existing ORM solutions instead of coding your own. A list of these can be found at http://api.mongodb.org/python/current/tools.html
Example: