davidlatwe / montydb

Monty, Mongo tinified. MongoDB implemented in Python !
BSD 3-Clause "New" or "Revised" License
578 stars 29 forks source link

how to set the storage engine to 'flat-file' in BSON format ? #95

Open jackywu opened 1 month ago

jackywu commented 1 month ago

I guys, I can't find a relevant info for that from docs. how to set the storage engine to 'flat-file' in BSON format ?

and this one raise exception.

from montydb import set_storage, MontyClient

set_storage("default.db", use_bson=True)  # optional step
client = MontyClient("default.db")  # use current working dir if no path given

# >>> from montydb import MontyClient
col = client.db.test
col.insert_many([{"stock": "A", "qty": 6}, {"stock": "A", "qty": 2}])
cur = col.find({"stock": "A", "qty": {"$gt": 4}})
print(list(cur))

➜  montydb-demo .venv/bin/python main.py
Traceback (most recent call last):
  File "/Users/jackywu/Repository/demo/montydb-demo/main.py", line 5, in <module>
    client = MontyClient("default.db")  # use current working dir if no path given
             ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jackywu/Repository/demo/montydb-demo/.venv/lib/python3.12/site-packages/montydb/client.py", line 41, in __init__
    storage_cls = provide_storage(repository)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jackywu/Repository/demo/montydb-demo/.venv/lib/python3.12/site-packages/montydb/configure.py", line 269, in provide_storage
    _bson_init(_session["use_bson"])
  File "/Users/jackywu/Repository/demo/montydb-demo/.venv/lib/python3.12/site-packages/montydb/configure.py", line 279, in _bson_init
    bson.init(use_bson)
  File "/Users/jackywu/Repository/demo/montydb-demo/.venv/lib/python3.12/site-packages/montydb/types/bson.py", line 67, in init
    from ._bson import BSON_
  File "/Users/jackywu/Repository/demo/montydb-demo/.venv/lib/python3.12/site-packages/montydb/types/_bson.py", line 5, in <module>
    class BSON_(types.ModuleType):
  File "/Users/jackywu/Repository/demo/montydb-demo/.venv/lib/python3.12/site-packages/montydb/types/_bson.py", line 7, in BSON_
    from bson import (
ImportError: cannot import name 'SON' from 'bson' (/Users/jackywu/Repository/demo/montydb-demo/.venv/lib/python3.12/site-packages/bson/__init__.py)

``

besides, can I set a user and password to protect the connection to the local falt-file db file?
davidlatwe commented 1 month ago

Hi @jackywu , do you have pymongo installed for accessing their bson module?

Although, by looking at your ImportError, you do have bson package in your venv. Which is strange to me that SON class was not able to import.

I just tested the code snippet you provided with a clean Python 3.12 venv and I got no error.


besides, can I set a user and password to protect the connection to the local falt-file db file?

Unfortunately, no. There are no actual server running in between your data to provide such protection.