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 = '{}'")
Related
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.
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")
I have a jsonb column named 'available_quantity'. which will have sample values
{ "100": 50, "1000":10 }
Now, I want to query all keys with values less than 50.
I tried this query,
Bundle.where('available_quantity #> ?', {'100': 5}.to_json)
But this one gives me all the Bundle with available_quantity containing {100: 5}.
How can I do that? Is that even possible?
You can use the ->> operator:
Bundle.where("(available_quantity->>'100')::int < 50")
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')
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].