Add multiple tinyDB databases together

482 views Asked by At

How do I add multiple tinyDB (document based database) databases together without getting a AssertionError error? I'm trying to add in this example 12 tinyDB databases together.

File structure:
enter image description here

Every numbered file looks likes this:

{
    "_default": {
        "1": {
            "Strategy": "MAShift",
            "Symbol": "AAVE/USD",
            "Timeframes": [
                "30T"
            ],
            "Parameters": {
                "atr": 14,
                "sma": 5,
                "longLine": 3,
                "shortLine": 5,
                "slMultipier": 12,
                "leverage": 1
            },
            "Start": "2020-10-13 12:00:00",
            "End": "2021-04-26 11:30:00",
            "Duration (days)": 194,
            "Equity Start [$]": 10000,
            "Equity Final [$]": 90470.5732,
            "Return [%]": 804.71,
            "Max. Drawdown [%]": -28.1,
            "Win rate [%]": 69.12,
            "Total trades": 570,
            "Avg. trade [%]": 0.43,
            "Avg. winning trade [%]": 1.88,
            "Avg. losing trade [%]": -2.81
        }, ...
    }
}

My code:

from tinydb import TinyDB

resultsTotalDb = TinyDB(f'db/backtestingResultsTotal.json')

for i in range(12):
    resultsDb = TinyDB(f'db/backtestingResults{i}.json')
    for result in resultsDb.all():
        resultsTotalDb.insert(result)

Error:

AssertionError: doc_id 1 already exists

1

There are 1 answers

0
AKX On BEST ANSWER

You can recompute a new document ID based on the database counter:

from tinydb import TinyDB
from tinydb.table import Document

# ...

for i in range(12):
    resultsDb = TinyDB(f"db/backtestingResults{i}.json")

    for result in resultsDb.all():
        new_id = i * 100000000 + result.doc_id
        resultsTotalDb.insert(Document(result, doc_id=new_id))