-
-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Question] How do we query to filter based on 2+ array items on the front-end? #482
Comments
MongoDB allows to query a array with exact equality but in that case it respect the specific order of the items in the array. You can do this if you are sure about the order of the items: query: {
tag: ['Coding', 'FeathersJS']
} Or you can use the $all operator like that: query: {
tag: { $all: ['Coding', 'FeathersJS'] }
} but you might need to whitelist the |
@J3m5 Thanks a lot!! I just stumbled on it as well. Ended up digging around in the code, found out that sift is being used to filter the results and the
Can I safely whitelist However, even after whitelisting both, the results are not the same when passed to the find getter ($all works as expected but $and just returns everything without any filtering). Is that expected? |
It depends on what you want your users to be able to do with your data, but for $and I would say yes, but there might be a risk of data leak with the $populate operator for example.
You should try this |
If I add it to the
Why do the top-level
Oh ok - Though I am not using any $populate, but it sounds like something that might cause future problems. Will try and simplify my stories for now and get back to this once I have a better understanding I guess. |
I think it's because of this: const customOperators = Object.keys(q).filter(
k => k[0] === '$' && !defaultOps.includes(k)
)
const cleanQuery = _omit(q, customOperators) The filtering doesn't take the whitelist into account, I've seen it before but I don't know why it's done because the |
When the whitelist was implemented, it was not added in the filter. |
Yeah I get what is happening in that flow - just don't understand why 😅 Hopefully, someone else can shed some light on this! |
I think that we can safely remove this code block because the |
@J3m5 Oh perfect - This would make my life a lot easier for sure if it can be safely removed! |
I would really appreciate a PR if this will benefit users. I'm a bit strapped for time for the next week or so, but I can review PRs and do releases. Thanks guys! |
@marshallswain @J3m5 started looking into how to contribute and get this fixed (I've never done a PR before so trying to figure it out). Along the way, I ran into some test cases that might need some fixing as well -
If I remove the code for clean query from the service getters, 2 of the test cases fail -
and
The first test case shouldn't pass at all given $populateQuery is not whitelisted (unless I am missing something) - It passes only because the existing cleanQuery code removes it and the final query that gets executed becomes The 2nd test case passes right now because
My experience with mocha hasn't been that much so don't know I missed something.
With those changes, the issue gets fixed and all test cases get passed. (Just for reference, here is the change in the find function in module.getters -
Let me know if this looks OK. I am still trying to figure out how to send a PR for this (Can both test-cases edit and code edits be part of the PR?) |
This is intented to fix the issue uncovered in #482. It also adds `$populateParams` to the `paramsForServer` by default, which means it now supports feathers-graph-populate out of the box, now.
@kshitizshankar thanks for the details in your last comment. I was able to make the PR and have published it as |
Considering a simple use case where I have the following records of Books loaded into Vuex store of the Books Service. Each book contains a list of
tags
.Book 1
Coding
Javascript
Education
Book 2
Javascript
Coding
FeathersJS
Book 3
Javascript
FeathersVuex
FeathersJS
Coding
How do I query for books that have
Coding
&FeathersJS
tags?This gives results for the books that have the
Coding
orFeathersJS
tags as expected. (Book1, Book2, Book3)I am not sure how to query for books that contain both the tags.
On the backend with mongoose, I can use the
$all
operator to achieve this - but how do I go about doing this on the frontend?Currently, my workaround was to have a custom getter on the service which executes the
$in
query and then loops through the results to find the items where both tags are present.Is there a better way to do this? Did I miss something w.r.t querying?
The text was updated successfully, but these errors were encountered: