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.
Related
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")
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 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 order query results by an hstore attribute?
#items = Item.includes(:product).order('products.properties #> hstore("platform")')
Causes
PG::Error: ERROR: column "platform" does not exist
LINE 1: ...oduct_id" ORDER BY products.properties #> hstore("platform"...
platform is a hstore key, stored in the properties column, which is an hstore type.
Double quotes are used to quote identifiers (such as table and column names) in PostgreSQL (and other databases that follow the standard). So when you say:
hstore("platform")
PostgreSQL sees "platform" as a quoted column name and since there is no platform column, you get an error.
Strings in standard SQL are quoted with single quotes, you want to say:
.order("products.properties #> hstore('platform')")
This will probably still fail though, hstore('platform') doesn't make much sense and neither does using #> here; a #> b means
does the hstore a contain the hstore b
If you're trying to sort on the value of the 'platform' key in the properties hstore then you'd want to use -> to lookup the 'platform' key like this:
.order("products.properties -> 'platform'")
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].