davidlatwe / montydb

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

Count documents #42

Open rewiaca opened 3 years ago

rewiaca commented 3 years ago

How to properly just to count documents?

x = col.find()
x.count()  # is deprecated and for Python3.5 causing error "The JSON object must be str, not "bytes"
x = col.count() #  Python3.5 causing error "The JSON object must be str, not "bytes"
x = cound_documents() # Says missing argument filter and then item. Why it does required?
davidlatwe commented 3 years ago
# Just give it an empty filter
x = col.cound_documents({})

Counting documents with a query filter is actually why cound_documents works more solid than count. https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.count_documents https://stackoverflow.com/a/52236791/4145300

And, Python 3.5 is not used in tests, so you might have to use newer Python version (3.6+)

rewiaca commented 3 years ago

And, Python 3.5 is not used in tests, so you might have to use newer Python version (3.6+)

Maybe it will be more helpful to specify this. For now I see: Requires: Python !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, >=2.7

Python 3.5.3 has such error with a lot of cursor operations which 3.7 is supporting:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/montydb/cursor.py", line 365, in next
    if len(self._data) or self._refresh():
  File "/usr/local/lib/python3.5/dist-packages/montydb/cursor.py", line 354, in _refresh
    self.__query()
  File "/usr/local/lib/python3.5/dist-packages/montydb/cursor.py", line 311, in __query
    for doc in documents:
  File "/usr/local/lib/python3.5/dist-packages/montydb/storage/lightning.py", line 253, in <genexpr>
    docs = (self._decode_doc(doc) for doc in self._conn.iter_docs())
  File "/usr/local/lib/python3.5/dist-packages/montydb/storage/__init__.py", line 229, in _decode_doc
    codec_options=self._collection.coptions
  File "/usr/local/lib/python3.5/dist-packages/montydb/types/_bson.py", line 280, in document_decode
    object_pairs_hook=lambda pairs: cls.object_hook(dcls(pairs), opts),
  File "/usr/lib/python3.5/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
davidlatwe commented 3 years ago

Ah, yes, you are right.. The Python requirement specification is not clear indeed, sorry about that. :(

Okay, had a quick look at the code and I think if you pip install -U montydb to the current latest version (2.3.8), you should be able to work on Python 3.5 if you have pymongo installed.

Details

The error you have, is related to how montydb encode/decode documents from storage. Based on the error traceback, it seems your montydb instance is working without using bson, which can only encode/decode documents with built-in json module.

If you update montydb to current latest version 2.3.8, it should correctly opt-in bson and starts to encode/decode documents with bson.BSON, which, should support Python 3.5.