I saw the concept of "docType" in the hyperledger fabric example chaincode "marbles_chaincode.go" and the code simply states "docType is used to distinguish the various types of objects in state database". I thought docType was a concept of couchdb, but I searched couchdb's documentation and found no introduction to docType. My question is, what exactly does "docType" do in Fabric? What is used for specifically? More specifically, is it a best practice to set the docType when I write a chaincode to store data on the chain, and why?

1 Answers

kajuken On Best Solutions

It's 'simply' used to help you sort and structure your assets while using couchdb as your state database.

Think of having multiple organisations selling different kind of goods on a market place based on fabric. These kinds could range from vehicles to real estates etc. To easily query and process the assets representing the goods you label them with the doctype. Doctypes help creating indexes which in return will be able to be queried in a way faster manner.

Notice that both the fields in the query, docType and owner, are included in the index, making it a fully supported query. As a result this query will be able to use the data in the index, without having to search the full database. Fully supported queries such as this one will return faster than other queries from your chaincode.

This above part is taken from the CouchDB Tutorial and maybe helps you giving a clearer understanding on why and when to use doctypes. Basically it is what you already thought. It can enable faster and easier handling of assets in the end.