简体   繁体   中英

Hyperledger fabric query by key

I am using Hyperledger Fabric node js sdk to develop an client application interacting with a hyperledger fabric network. I am storing multiple assets as key-value pair storing asset 'x' with key 'x_###', for asset 'y' with key 'y_###' and so on. I wanted to query the world state by key to get all the asset that starts with for example, 'x_' to get all the x assets. How can I achieve this? TIA.

You can use key range queries or key composite queries for this purpose. For your scenario, I'd recommend composite key queries, where 'x' is one part of the composite key and '###' is the other part of the composite key (no need for the underscore separator, as Fabric implicitly adds a null character as the separator in composite keys). This allows you to query for any key having 'x' as the first composite key part.

To see an example, look at the ledger queries sample chaincode , search for the CreateCompositeKey() and GetStateByPartialCompositeKey() function calls.

Note, key range queries and key composite queries are supported when using either LevelDB or CouchDB as the state database. CouchDB in only required if you want to query the JSON value content, not the key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM