<p>I used a tutorial online to create my own block explorer (<a href="https://www.youtube.com/watch?v=laeV2slJgc8" rel="nofollow noreferrer">https://www.youtube.com/watch?v=laeV2slJgc8</a>). </p>

<p>In this tutorial, I used "Iquidus" to create the block explorer, which uses MongoDB and Node.js. Now I want to make my own API call that will list all UTXOs for a given address.</p>

<p>Is there a MongoDB query I can just run to get the UTXOs or What would the code even look like to do something like that?</p>

<p>The call I am talking about is the same they have on "<a href="https://chainz.cryptoid.info/api.dws" rel="nofollow noreferrer">https://chainz.cryptoid.info/api.dws</a>":</p>

<p>"<strong>unspent</strong> becomes available, this API is similar to blockchain.info's, it has a single active parameter in which you can pass multiple addresses separated by '|'. It returns information about unspent outputs of those addresses."</p>

<p>MongoDB is a schema-less database but I managed to get this:</p>

<pre><code>&gt; show collections
addresses
coinstats
markets
peers
richlists
txes
&gt; var col_list = db.addresses.findOne(); for (var col in col_list) { print (col) ; }
_id
a_id
balance
sent
received
txs
__v
&gt; var col_list = db.coinstats.findOne(); for (var col in col_list) {print (col) ; }
_id
coin
last_price
connections
supply
last
count
__v
&gt; var col_list = db.markets.findOne(); for (var col in col_list) {print (col) ; }
_id
market
history
sells
buys
chartdata
__v
&gt; var col_list = db.peers.findOne(); for (var col in col_list) {print (col) ; }
&gt; var col_list = db.richlists.findOne(); for (var col in col_list) {print (col) ; }
_id
coin
balance
received
__v
&gt; var col_list = db.txes.findOne(); for (var col in col_list) {print (col) ; }
_id
txid
blockhash
blockindex
timestamp
total
vout
vin
__v
&gt; 
</code></pre>

<p>Desired output</p>

<pre><code>{"unspent_outputs":[{"tx_hash":"819d2c6331fecf0bcda79962ed7b66c71b508a4698680b84310f4d2f67203e87","tx_ouput_n":0,"value":100000,"confirmations":63499,"script":"76a9142beec605c9a6512f55fe93ad76753e24fc8579b088ac","addr":"MBuTKxJaHMN3UsRxQqpGRPdA7sCfE1UF7n"},{"tx_hash":"819d2c6331fecf0bcda79962ed7b66c71b508a4698680b84310f4d2f67203e87","tx_ouput_n":1,"value":767048,"confirmations":63499,"script":"76a9142beec605c9a6512f55fe93ad76753e24fc8579b088ac","addr":"MBuTKxJaHMN3UsRxQqpGRPdA7sCfE1UF7n"},{"tx_hash":"7d76cc54c510f1e8f4dd4dbf4d215713245cb3fd7ec1417f852b7d34deee116f","tx_ouput_n":0,"value":1000000000,"confirmations":63439,"script":"76a9142beec605c9a6512f55fe93ad76753e24fc8579b088ac","addr":"MBuTKxJaHMN3UsRxQqpGRPdA7sCfE1UF7n"},{"tx_hash":"03033110cbe81f34c3eede0affd626a135ccea67e783f2beaf02c98ba2e721f3","tx_ouput_n":0,"value":314419,"confirmations":10977,"script":"76a9142beec605c9a6512f55fe93ad76753e24fc8579b088ac","addr":"MBuTKxJaHMN3UsRxQqpGRPdA7sCfE1UF7n"}]}
</code></pre>

CC BY-SA 4.0
2019-08-26T23:27:21.487Z
can you provide the database schema?
JBaczuk
CC BY-SA 4.0
2019-08-26T23:56:58.697Z
I edited my question, I hope it helps. Thank you for the reply. @JBaczuk
IGOR
CC BY-SA 4.0
2019-08-27T00:02:48.097Z
Where does the explorer get this data? Does it sync from a Bitcoin Core node? If so, why not just adjust the explorer to sync the latest UTXO set? It will be very difficult to create a query that gets unspent outputs for a given address because it will have to traverse the entire blockchain to determine which outputs are unspent.
JBaczuk
CC BY-SA 4.0
2019-08-27T00:09:01.393Z
yes, the explorer is using the core wallet to get its data and places that data into the database. Not really sure how to adjust the explorer to sync the latest UTXO set, thats why I assumed its possible to make a query of some sort, that would query the database to get that information.
IGOR
CC BY-SA 4.0
2019-08-27T00:20:24.957Z
In order to execute that query you have to search the entire blockchain for every output to any address that you own. And for each of those outputs you have to search from and including the block with that output to the latest block to see if it has been spent or not. Bitcoin core does the last part for you by maintaining a database of unspent outputs.
JBaczuk
<bitcoin-core><blockchain><bitcoind><database><node.js>
Creating a "listunspent" API call using MongoDB
IGOR
# Creating a "listunspent" API call using MongoDB

(Asked by: username_0 on 2019-08-26)

I used a tutorial online to create my own block explorer (https://www.youtube.com/watch?v=laeV2slJgc8). 
In this tutorial, I used "Iquidus" to create the block explorer, which uses MongoDB and Node.js. Now I want to make my own API call that will list all UTXOs for a given address.
Is there a MongoDB query I can just run to get the UTXOs or What would the code even look like to do something like that?
The call I am talking about is the same they have on "https://chainz.cryptoid.info/api.dws":
"unspent becomes available, this API is similar to blockchain.info's, it has a single active parameter in which you can pass multiple addresses separated by '|'. It returns information about unspent outputs of those addresses."
MongoDB is a schema-less database but I managed to get this:
> show collections
addresses
coinstats
markets
peers
richlists
txes
> var col_list = db.addresses.findOne(); for (var col in col_list) { print (col) ; }
_id
a_id
balance
sent
received
txs
__v
> var col_list = db.coinstats.findOne(); for (var col in col_list) {print (col) ; }
_id
coin
last_price
connections
supply
last
count
__v
> var col_list = db.markets.findOne(); for (var col in col_list) {print (col) ; }
_id
market
history
sells
buys
chartdata
__v
> var col_list = db.peers.findOne(); for (var col in col_list) {print (col) ; }
> var col_list = db.richlists.findOne(); for (var col in col_list) {print (col) ; }
_id
coin
balance
received
__v
> var col_list = db.txes.findOne(); for (var col in col_list) {print (col) ; }
_id
txid
blockhash
blockindex
timestamp
total
vout
vin
__v
> 

Desired output
{"unspent_outputs":[{"tx_hash":"819d2c6331fecf0bcda79962ed7b66c71b508a4698680b84310f4d2f67203e87","tx_ouput_n":0,"value":100000,"confirmations":63499,"script":"76a9142beec605c9a6512f55fe93ad76753e24fc8579b088ac","addr":"MBuTKxJaHMN3UsRxQqpGRPdA7sCfE1UF7n"},{"tx_hash":"819d2c6331fecf0bcda79962ed7b66c71b508a4698680b84310f4d2f67203e87","tx_ouput_n":1,"value":767048,"confirmations":63499,"script":"76a9142beec605c9a6512f55fe93ad76753e24fc8579b088ac","addr":"MBuTKxJaHMN3UsRxQqpGRPdA7sCfE1UF7n"},{"tx_hash":"7d76cc54c510f1e8f4dd4dbf4d215713245cb3fd7ec1417f852b7d34deee116f","tx_ouput_n":0,"value":1000000000,"confirmations":63439,"script":"76a9142beec605c9a6512f55fe93ad76753e24fc8579b088ac","addr":"MBuTKxJaHMN3UsRxQqpGRPdA7sCfE1UF7n"},{"tx_hash":"03033110cbe81f34c3eede0affd626a135ccea67e783f2beaf02c98ba2e721f3","tx_ouput_n":0,"value":314419,"confirmations":10977,"script":"76a9142beec605c9a6512f55fe93ad76753e24fc8579b088ac","addr":"MBuTKxJaHMN3UsRxQqpGRPdA7sCfE1UF7n"}]}

**Tags:** bitcoin-core, blockchain, bitcoind, database, node.js

> **Comments:**
> **username_1**: can you provide the database schema?  
> **username_0**: I edited my question, I hope it helps. Thank you for the reply. @username_1  
> **username_1**: Where does the explorer get this data? Does it sync from a Bitcoin Core node? If so, why not just adjust the explorer to sync the latest UTXO set? It will be very difficult to create a query that gets unspent outputs for a given address because it will have to traverse the entire blockchain to determine which outputs are unspent.  
> **username_0**: yes, the explorer is using the core wallet to get its data and places that data into the database. Not really sure how to adjust the explorer to sync the latest UTXO set, thats why I assumed its possible to make a query of some sort, that would query the database to get that information.  
> **username_1**: In order to execute that query you have to search the entire blockchain for every output to any address that you own. And for each of those outputs you have to search from and including the block with that output to the latest block to see if it has been spent or not. Bitcoin core does the last part for you by maintaining a database of unspent outputs.  

---

## 0 answers

