Grouping and Counting arrays in rethinkdb

713 views Asked by At

I have json data as follows:

{

"artist": {
    "aliases": {
        "name": "Thaddeuz Anderson"
    } ,
    "data_quality": "Correct" ,
    "id": 2377 ,
    "name": "DJ Thadz" ,
    "namevariations": {
        "name": [
            "D. J. Thadz" ,
            "D.J. Thadz" ,
            "DJ Thaz" ,
            "Thadz"
        ]
    } ,
    "profile": "" ,
    "realname": "Thaddeuz Anderson"
} ,
"id": "040a5506-5d4a-4beb-a27b-90f7cf3b0a69"

 }
 {

"artist": {
    "data_quality": "Correct" ,
    "id": 2273 ,
    "name": "Riff HiFi" ,
    "namevariations": {
        "name": "Riff Hiffi"
    } ,
    "profile": ""
} ,
"id": "041bdd4d-f7ae-4bb0-b971-88f252281421"

 } 

I want to find the number of aliases for every artist.

So I am using the following command:

r.table('artists')('artist')('aliases').group('name').count()

The result I get is 1 in reduction. How do I get the total number of elements in the aliases array? Please help

1

There are 1 answers

6
Jorge Silva On BEST ANSWER

Whenever you group something, you need to then ungroup it. So, for your query, you need to add ungroup at the end.

r.table('artists')('artist')('aliases').group('name').count().ungroup()

That being said, If all you want is get the number of aliases for every artist, group is not really the method for that. You're better of using merge to append an alias count to the document.