I'm trying to run a quarry with an inequality and multiple array-contains.
For simplicity, I simplified the data structure with imaginary data. Let's say in a collection called grocerie_packages I have many documents. I want to be able to proved searches based on the name of the package, the price of the package and the vegetables and fruits it contains. The current data structure is the following:
{
price: 10,
contained_fruits: ["apple", "pear", "grape"],
contained_vegetables: ["tomato", "cucumber", "carrot"],
package_name: ["f", "fa", "fam", "fami", "famil", "family", "family_", "family_s", "family_si", "family_siz", "family_size"]
}
At first, I used the following code to query based on the name:
.where("package_name", ">=", searchString)
.where("package_name", "<=", searchString + "\uf8ff");
But later, when I added the functionality of querying by price I ran into the issue of:
Invalid query. All where filters with an inequality (<, <=, >, or >=) must be on the same field.
I solved it by creating an array of the substrings of package_name. The code I used was something like this:
.where("package_name", "array-contains", package_name_intput)
.where("price", "<", max_price);
The problem I'm having is that now, since I did one array-contains to the query I can't query based on the contained vegetables and fruits. I don't want to sort on the client, since there are cases when I would return potentially thousands of items more than needed. I also can't combine the three array into one, because I only want to return a document if it matches all criteria of the query. For instance in a query where someone asks for apple and carrot I only want to return documents which contains both. If it was one array it would return the document even if only one item is found in the package. I only need to provide one item (like apple) search per vegetables and fruits.
What is the best way for me to run a query like this? I'm trying to find a solution where I don't fill the document with many-many flags just for querying porpuses. For instance:
{
flags: ["apple_tomato_f", "apple_tomato_fa", "apple_tomato_fam", ] //and so on
}
from Do an inequality and multiple "array-contains", or array-contains-like filters
No comments:
Post a Comment