Strange behaviour of pymongo


I’m looking for help for the following problem:

I develop a blockchain web application in Python 3.7, using MongoDB as a backend, so obviously I use the pymongo driver and Flask as well.

If I collect the data as elements of a response of a web request and write it to the DB (but not reading from it), the ObjectID becomes the part of the response (in-memory) and get an error 500 as the ObjectID - of course - is not a JSON serializable. I already tried to define a json decoder but it didn’t have any affect. I also tried to use a different variable and call the DB write operation from a completely separated module but the ObjectID still becomes the part of the in-memory response. So all in all, I’m suspecting that pymongo tricks me.

Do you have any recommendation, where to go from now? I really appreciate any recommendation!

Kind regards,

ps: I do apologize if my english is poor, I’m not a native speaker.

Can you post the code here please. That would help.

Here is the relevant code snippet:

block = {
            'index': len(self.chain) + 1,
            'timestamp': time(),
            'transactions': self.current_transactions,
            'proof': proof,
            'previous_hash': previous_hash or self.hash(self.chain[-1]),

        # Reset the current list of transactions
        self.current_transactions = []
        data = block
        # chainColl.insert_one(block)
        return block

You can see above, I tried to insert the “block” data directly from here (“chainColl” is the collection name in mongo) but then the “return block” contained the ObjectID. Then I wrote my own module (dbWriter) and used a completely different variable “data” but ObjectID also got into “block” after writing into the DB.