Let's say I want to create a Solidity contract:

contract Book {
 string title;
 string author;
 function setTitle() {}
 function setAuthor() {}

and I want people to be able to call this contract to store different book titles and authors like Hamlet and Shakespeare. What makes the most sense as far as how to architecture this if I want to be able to later filter over all the books that have been stored for let's say a given author like a Book Explorer of sorts?

  • :) Ethereum Blockchain is not a database to store and filter data, because it's slow and costs alot. It is more a place for minimal and critical logic
    – Majd TL
    Jun 18, 2021 at 9:51

1 Answer 1


This excellent blog on Simple Storage Patterns in Solidity should give you a clear and concise idea to adopt your preferred pattern.

One of the simpler approaches is using a solidity mapping to store key-value pairs, where author names are paired with a list of book titles. The contract might look something like this:

contract Book{

    string title;
    string author;

    mapping (string => string[]) books;

    function setAuthorAndTitle(string _author,string _bookTitle){

    function getBooksByAuthor(string _author) public view returns (string[]) {
        string[] memory titles = books[_author];
        return titles;

NOTE: You might be asked to include "pragma experimental ABIEncoderV2;" statement by the compiler, as it's an experimental feature in solidity

Also make sure to keep index / counter variables and proper conditional checks to have the edge cases handled as per your scenario.

If you plan to add more attributes and functionalities, you can also create a mapping of structs to handle that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.