How to filter by Array in Vapor 4? - vapor

I can do a filter in Vapor 4 like this
Category.query(on: req.db)
.filter(\.$description ~~ searchTags)
description here is a String. And it will match description to any of the strings in the searchTags array.
But what if description was also an array? Is there a way to match all the values in descriptionArray to the searchTags array?

If description was an array, then it should be made into an array of child objects, this way you can search for using a simple filter on description and parent id.

Related

OData filter objects that contain particular items in subarray

I have the following situation:
I have an object
This object has an item that is an object again
The second object contains array of objects with ids
Can I filter only the parent objects that have subobjects that contain something in particular in the array on the bottom of the tree?
Try this:
$filter=nameOfObjectArrayProperty/any(o: contains(o/id, 'some-alue'))
or
$filter=nameOfObjectArrayProperty/any(o: o/id eq 'some-value')
It's also important to note that "/any" can be replaced with "/all" if you want all the subobjects to match your filter criteria.

Rails 4 Active record, search by attribute that contains an array?

I want to search by an attribute that contains an array. I'm interested in returning all records where the array in this attribute contains a specific value.
example object
Location_1 {
regions: ["on", "qc"]
}
I want to do something like this Location.where(regions: "on"), but I'm not sure of the correct syntax.
what is the right way to do this?
Try this Location.where('regions in (?)', ['on','qc'])
The IN operator allows you to specify multiple values in your WHERE clause.

How do I extract a field from an array of my models in order to form a single query?

I’m using Rails 4.2.7. I have an array of my model objects and currently I’m iterating through that array to find matching entries in the database based on a field my each object …
my_object_times.each_with_index do |my_object_time, index|
found_my_object_time = MyObjectTime.find_by_my_object_id_and_overall_rank(my_object_id, my_object_time.overall_rank)
My question is, how can I rewrite the above to run one query instead of N queries, if N is the size of the array. What I wanted was to force my underlying database (PostGres 9.5) to do a “IF VALUE IN (…)” type of query but I’m not sure how to extract all the attributes from my array and then pass them in appropriately to a query.
I would do something like this:
found_my_object_times = MyObjectTime.where(
object_id: my_object_id,
overall_rank: my_object_times.map(&:overall_rank)
)

Mongoid order by length of array

How to sort the Mongoid model by the length of the array which is a field inside the model.
Mongo documentation says:
You cannot use $size to find a range of sizes (for example: arrays
with more than 1 element). If you need to query for a range, create an
extra size field that you increment when you add elements. Indexes
cannot be used for the $size portion of a query, although if other
query expressions are included indexes may be used to search for
matches on that portion of the query expression.
So we cannot order by using mongo's $size.
You can solve your task by adding new field, which will store array size.
class Post
include Mongoid::Document
field :likes, type: Array, default: []
field :likes_size, type: Integer
before_save do
self.likes_size = likes.size
end
end
Sort posts by likes_size:
Post.order_by(likes_size: :desc)
Document says that you can't orderby using size.
Try adding a new column containing the value of size and sort it which will work as order by.
In ruby, you can sort an array like this :
my_array.sort_by(&:my_attr)
It will sort the array my_array by the attribute my_attr of each element inside the array.
You can also write it like this :
my_array.sort_by{|element| element.my_attr }
Which is exactly the same, it will sort by the my_attr attribute of each element. This second syntax is for when you want a more complex sort condition than just the result of a method of each element.
Documentation : http://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-sort_by

What is simplest way to convert :pluck or :collect results to array of strings in Rails?

I have a model called Record and belongs Product model,
the price column type is hexadecimal. Rails already convert it to string in view. But I wanna get them in my console queries.
Example code for pluck:
Product.first.records.pluck(:price)
This query displays the values in array as hexadecimal. There is a method called to_sentences for pluck values but its not enough for my case.
The problem is same in collect method:
Product.first.records.collect(&:price)
What is the pluck query to display my hexadecimal data as array of strings like:
["45,46","75,42"]
Product.first.records.pluck(:price).map(&:to_s)
please try this:
Product.first.records.pluck(:price).join(',')

Resources