Understanding Rich Queries

There are two options of state databases available in Hyperledger Fabric: LevelDB and CouchDB. While LevelDB is a simple and lightweight key-value store, CouchDB is a document-oriented NoSQL database. Both databases provide all the basic querying functionality, including key, key range, and composite key queries. However, if you model and store assets in the JSON format in CouchDB, you can also create indexes and perform complex rich queries against the chaincode data values. For this purpose, you need to utilize the CouchDB JSON query language within your chaincode.

Rich queries are excellent for understanding what is going on in the ledger. Proposal responses for these types of queries are typically useful to the client application, but are not typically submitted as transactions to the ordering service. In fact, there is no guarantee the result set is stable between chaincode execution and commit time for rich queries. Therefore, rich queries are not appropriate to use in the update transactions, unless your application can ensure the stability of the result set between chaincode execution and commit time or can handle potential changes in the subsequent transactions. For example, if you perform a rich query for all assets owned by Alice and transfer them to Bob, a new asset may be assigned to Alice by another transaction between chaincode execution and commit time, and you will miss this “phantom” item.

Last updated

Was this helpful?