pyMongoDB bson import issue

I am testing writing to MongoDB.

sys.path.append(r"HIDE\pyRevitEnv\lib\site-packages")
from pymongo import MongoClient

I get an error


CPython Traceback:

ImportError : cannot import name 'SON' from 'bson' (\pyRevit-Master\site-packages\bson\__init__.py)
File "\Assembly.tab\Export.panel\mongoDB.pushbutton\script.py", line 13, in <module>
from pymongo import MongoClient
File "\pyRevitEnv\lib\site-packages\pymongo\__init__.py", line 91, in <module>
from pymongo.common import MAX_SUPPORTED_WIRE_VERSION, MIN_SUPPORTED_WIRE_VERSION, has_c
File "\pyRevitEnv\lib\site-packages\pymongo\common.py", line 39, in <module>
from bson import SON

pyRevitLabs.PythonNet
at Python.Runtime.Runtime.CheckExceptionOccurred()
at Python.Runtime.PyScope.Exec(String code, IntPtr _globals, IntPtr _locals)
at Python.Runtime.PyScope.Exec(String code, PyDict locals)
at PyRevitLabs.PyRevit.Runtime.CPythonEngine.Execute(ScriptRuntime& runtime)

It appears that bson is part of pyRevit-Master, which does not include SON for import.

ChatGPT had me try uninstall and reinstall in my enve. I did and can import pymongo when in VSCode. This means it is working in the venv. Which I think narrows the issue to be a conflict with the bson in the pyRevit-Master.

ChatGPT suggested searching in vsCode “from bson” on the folder structure of the pyRevit-master to see where it is being referenced. I did this but did not see any import references. If not then Chat suggests removing from the folder.

I am unsure if that is a bad idea.
Does anyone know what in pyRevit depends on that bson package?

EDIT:
I renamed the bson folder in the pyRevitmaster and was able to load pyMongo in a script. Hence there is an issue with pyMongo in the env and the bson lib in the pyRevitmaster.

I think SON is not supported in IronPyhon (or python 2.7).
You might try to use collections.OrderedDict as an alternative, but I have not tried this myself, so it might not work.
Or you could try to write your script in CPython. (But CPython in pyRevit is not yet totally stable.)

ChatGPT is “always” trying to use Python 3, even if you are using custom GPTs like “PyRevit Helper”, but using a custom GPT gives you code that is usually more aligned to work in pyRevit, evben if you have to remind it to write IronPython compatable code.

I have success in exporting to a MongoDB from within Revit.
I had a lot of help from ChatGPT, I’m not expert in this area.

I found an issue with using MongoDB in pyRevit, which required the following to get to work.

My version of pyRevit includes the bson libarary in the site-packages folder:
%user% AppData\Roaming\pyRevit-Master\site-packages\bson\

I change the name of the site-package bson folder

image

I did a full restart of Revit.
My script worked.

The code snip below includes some code that is relevant to pyMongo.
I have removed the logic to get the category_data from the script.

#!python3

from pymongo import MongoClient
from Autodesk.Revit.DB import BuiltInCategory

# Get the current Revit document
doc = __revit__.ActiveUIDocument.Document

# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/')
database_name = 'revit_data_test2'
db = client[database_name]

# Function to insert data into MongoDB
def insert_category_data(db, category_name, category_data):
    elements = category_data.get('Elements', {})
    types = category_data.get('Types', {})

    elements_collection = db[f"{category_name}_Elements"]
    types_collection = db[f"{category_name}_Types"]

    # Insert Elements
    for element_id, element_data in elements.items():
        document = {'_id': int(element_id)}
        document.update(element_data)
        elements_collection.replace_one({'_id': int(element_id)}, document, upsert=True)

    # Insert Types
    for type_id, type_data in types.items():
        document = {'_id': int(type_id)}
        document.update(type_data)
        types_collection.replace_one({'_id': int(type_id)}, document, upsert=True)

    # Insert data into MongoDB
    insert_category_data(db, category_name, category_data)