Filter entry query by a custom field value
Your code should work. Make sure that your Settings → Fields → Navigation has Use this field’s values as search keywords checked.
Once you're sure it is checked, run ./craft resave/entries --update-search-index and it should start working.
Note that instead of searching, you could use:
{% set nav = craft.entries()
    .section('pages')
    .navigation('mainNavigation')
    .all() %}