How to calculate coin supply at each block height from all transactions? We are working on a small project and imported all block information to mongoDB. Right now we're aiming to calculate the coin supply regarding to the block number.

My plan is to loop through all transactions per block and parse vout and vin. However, I'm not sure how to distinguish mined coins and previously existing coins transferred users. Plus, how to account for transaction fees?
Can I track only "valueOut" and that's all?

{
   "txid":"0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098",
   "version":1,
   "locktime":0,
   "vin":[
      {
         "coinbase":"04ffff001d0104",
         "sequence":4294967295,
         "n":0
      }
   ],
   "vout":[
      {
         "value":"50.00000000",
         "n":0,
         "scriptPubKey":{
            "hex":"410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac",
            "asm":"0496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858ee OP_CHECKSIG",
            "addresses":[
               "12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX"
            ],
            "type":"pubkeyhash"
         },
         "spentTxId":null,
         "spentIndex":null,
         "spentHeight":null
      }
   ],
   "blockhash":"00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
   "blockheight":1,
   "confirmations":601155,
   "time":1231469665,
   "blocktime":1231469665,
   "isCoinBase":true,
   "valueOut":50,
   "size":134
}


Any help would be appreciated!
The first transaction in every block is called the coinbase transaction, it is a special transaction that pays the miner their reward. This transaction adheres to different rules than the rest: it has no inputs, but has output(s). 

The coinbase transaction outputs are allowed to be equal to or less than the block subsidy (newly minted coins) + all transaction fees in the block. 

The block subsidy is a set amount, as defined by the code. The subsidy started at 50 BTC, and that amount is cut in half every  210,000 blocks (25, 12.5, 6.25, etc). 

The transaction fees for the block are found by taking the rest of the transactions in the block (ie everything that isn’t the coinbase transaction), and calculating the difference between the inputs and outputs in them. This value (input value - output value) is the implied fee. 

Note that there are some odd cases to account for, this question has a bunch of great info in this regard.  in addition to this, there is a non-zero amount of BTC that has been ‘burned’, by being sent to addresses or scripts that are un-spendable, or at least have an extremely small chance of someone actually knowing the private key for them (eg the 1BitcoinEater... address). Depending on how you define ‘coin supply’, it may be worth account for these coins as well.