Search code examples
hyperledger-composer

How can I get the total amount of assets use Query function?


As the tutorial of Query, I can use statement to query assets, participant like sql. But my question is can I query the assets use Count() to get the length of a specified assets?If it is not effective, how can I get the total amount or latest element of the assets?


Solution

  • Composer query language offers a very limited set of queries. An overview can be found at Hyperledger Composer Query Language. There you may see that this query language does not support any aggregation functions like count, sum, max, min, ... yet.

    The consequence is that you need to execute a query which filters your data properly to avoid huge arrays and then apply the respective aggregation in JavaScript as shown in the link david_k has given.

    For the latest element it might be e.g. possible to sort by id and then read the "last" entry of the result.