update_one and update_many creating extra records for flatfile
SEary342 opened this issue · comments
Performing updates with the flat file is giving me duplicate documents. I'm running Python 3.8.9 and have tried it with both the pip install montydb
install and pip install montydb[bson]
install. This problem is not occurring when in sqlite mode.
Subsequent program runs after inserting a record are causing a duplicate document to be added with the same _id.
It looks like the OrderedDict cache update at
https://github.com/davidlatwe/montydb/blob/master/montydb/storage/flatfile.py#L79 is where the extra document is being added. Debugging the process shows that Python is adding a duplicate document because the keys in the ordered dict are actually different. One is an ObjectId object and the other is the binary serialized representation of that id. Here is a screenshot of the debugging output:
Here is the source code to reproduce. Note that you will have to run it twice because this only occurs on subsequent runs.
from montydb import MontyClient, set_storage
set_storage("./db/repo", cache_modified=0)
client = MontyClient("./db/repo")
coll = client.petsDB.pets
if len([x for x in coll.find({"pet": "cat"})]) == 0:
coll.insert_one({"pet":"cat", "domestic?":True, "climate": ["polar", "equatorial", "mountain"]})
coll.update_one({"pet": "cat"}, {"$push": {"climate": "continental"}})
# This should only ever print 1 on subsequent runs.
print(len([x for x in coll.find({"pet": "cat"})]))
Hi @davidlatwe, thanks for the quick fix on this one. I just took a look at 2.8.9 and ran my test code against it. The bug appears to be fixed. 🎉