Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9091993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:44:18+00:00 2026-06-16T22:44:18+00:00

Does anyone know of a Python library that can convert a class object to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:44:19+00:00Added an answer on June 16, 2026 at 10:44 pm

    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.encode on 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:

    >>> import bson
    >>> class Example(object):
    ...     def __init__(self):
    ...             self.a = 'a'
    ...             self.b = 'b'
    ...     def set_c(self, c):
    ...             self.c = c
    ... 
    >>> e = Example()
    >>> e
    <__main__.Example object at 0x7f9448fa9150>
    >>> e.__dict__
    {'a': 'a', 'b': 'b'}
    >>> e.set_c(123)
    >>> e.__dict__
    {'a': 'a', 'c': 123, 'b': 'b'}
    >>> bson.BSON.encode(e.__dict__)
    '\x1e\x00\x00\x00\x02a\x00\x02\x00\x00\x00a\x00\x10c\x00{\x00\x00\x00\x02b\x00\x02\x00\x00\x00b\x00\x00'
    >>> bson.BSON.encode(e)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.7/dist-packages/bson/__init__.py", line 566, in encode
        return cls(_dict_to_bson(document, check_keys, uuid_subtype))
    TypeError: encoder expected a mapping type but got: <__main__.Example object at         0x7f9448fa9150>
    >>> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of a library - ideally Python, that can have a stab
Does anyone know if there's an available python library compatible with Python2.6 that exposes
Does anyone know if there is a Python library for text-to-speech that works on
Does anyone know of a python library that has DTW implementation? mlpy seems to
Does anyone know if Python's shelve module uses memory-mapped IO? Maybe that question is
Does anyone know any site or book that presents problems like python challenge ,
Does anyone know of any websites, or (preferably) downloadable packages that you can use
Does anyone know how to convert this function from Python to PHP? I have
Does anyone know of a library like chronic but for python? Thanks!
Does anyone know of a java version of the python deferred library? Or maybe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.