JSONB Querying array - ruby-on-rails

I have the following a column in a PostgreSQL table that has a JSONB field with data like:
[{"id":33,"url":"","name":"test"}, {"id":45,"url":"","name":"test"}]
I'm trying to query it and return the element that matches the id. I have the following query in Rails, but it doesn't seem to be matching:
Book.where('data #> ?', '[{"id": 33}]')

Since you only need some elements in the jsonb array, you will need to transform those into records and cross join them with their parents:
Book.joins(", jsonb_array_elements(books.data) obj")
.where("obj->>'id' = '33'")
.select("obj")

Related

MongoDB aggregate query equivalent to Ruby on Rails distinct query

Ruby on Rails distinct query returns array of unique values in specified field.
User.distinct(:name)
returns array of unique values in name field.
e.g. ['John', 'David', 'Steve']
Looking out for an equivalent aggregate query (User.collection.aggregate([])) in MongoDB which would return an array of unique values in specified field.

Return activerecord results for jsonb column array count greater than given value

In my rails 5 application with a Postgresql database, there is a class Listing with a jsonb column media.
I want to return the records that have array length greater than 10 in the media column.
How would I construct that query in ActiveRecord?
Turns out it is very straightforward
Listing.where('JSONB_ARRAY_LENGTH(media) > 10')

How do I search for '%stars%' within a Postgresql array?

I am trying to find a record in a Postgresql Query, where I have a column 'alternate_names'
t.text "alternate_names", default: [], array: true
And, I would like to find the following record:
altenate_names = ['All Stars']
where my criteria is 'Stars'
In other words, how can I write the following
select * from group where '%Stars%' = ANY(alternate_names);
But this won't return my record above because it has the array ['All Stars']...so, how can I perform an ILIKE '%stars%' with an array?
if you want LIKE operator:
select * from group where alternate_names::text like '%Stars%';
Comparing text representation of array against pattern is not effective, but wildcard before pattern won't be effective in any query on text[]
You can use unnest to check for partial match of all elements in an array.
Ex:
select * from group where exists (select 1 from unnest(alternate_names) as a_names where a_names like '%Stars%');

Rails Postgres how to query a jsonb column for empty objects?

How do I get all the empty records for a jsonb column with Active Record ?
You can query for empty objects in a JSONB column using the following syntax:
Model.where("column = '{}'")

How can i sort Cassandra-cql Results by timestamp?

Hi how can i sort my entries by the timestamp of the columns?
def self.get_users
##db.execute("SELECT * FROM users")
end
Do you mean timestamp of the values, or is your column key some kind of timestamp?
CQL doesn't support sort - it returns data in the order it is stored on disk. You tell Cassandra how you want you columns sorted by specifying the comparator_type on your Column Family. In this case, you might consider using a composite column keys of [timestamp, column name].

Resources