rethinkdbsecondary-indexes

RethinkDB's multi-indexes on nested field


I have a table with this document:

{
    "autoroles": [
        {
            "id": "305372902679642113",
            "users": [
                "262700032262799382",
                "166597257381150720",
                "149505704569339904",
                "203300837928206337"
            ]
        }
    ],
    "id": "275302446274838531",
    "name": "Some name...",
    "owner": "262700032262799382"
}

I want to retrieve documents who have a specific autoroles.users (for example 149505704569339904). So, I've created this multi index:

r.table('serv').indexCreate('users', r.row('autoroles')('users'), {multi: true})

Now, I try to get the document:

r.table('serv').getAll('149505704569339904', {index:'users'})

But this query return nothing.

I don't understand what I'm doing wrong since autoroles.users is an array, which is what a multi index should handle.

Thanks for your replies :)


Solution

  • Both your autoroles and users fields are arrays. So your index function returns an array of arrays. With your sample document:

    > r.row('autoroles')('users')
    [["262700032262799382", "166597257381150720", "149505704569339904", "203300837928206337"]]
    

    You can use concatMap instead to flatten that array in your index function:

    r.table('serv')
     .indexCreate('users', r.row('autoroles').concatMap(r => r('users')), {multi: true})