tminglei / slick-pg

Slick extensions for PostgreSQL
BSD 2-Clause "Simplified" License
838 stars 180 forks source link

Quick question on sorting jsonb column keys #320

Open iostreamdoth opened 7 years ago

iostreamdoth commented 7 years ago

How do I sort rows on the basis of a key in a jsonb type. My jsonb data looks like this, column name is RESULTS.

{"data":"a","count":"10"}
{"data":"b","count":"11"}
{"data":"c","count":"13"}
{"data":"d","count":"3"}

I want to use it like .sortBy(row =>row.results.data.asc)

I tried a query to do this in Postgres

select * from public."OPERATION" 
ORDER BY "RESULTS"->>'data' ASC 

and slick class has def results = column[Option[JsValue]]("RESULTS")

can we achieve this somehow through slick?

tminglei commented 7 years ago

Hi @nishantsharmajecrc you can try [theTableQuery].sortBy(_.results..+>>("data")). I just tried it here, it should be OK.

iostreamdoth commented 7 years ago

thanks @tminglei . That worked.