Skip to content
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

Sort and filter in population #109

Open
Pablo-Galbusera opened this issue Feb 26, 2020 · 1 comment
Open

Sort and filter in population #109

Pablo-Galbusera opened this issue Feb 26, 2020 · 1 comment

Comments

@Pablo-Galbusera
Copy link

Hi Loris,
I'm using your lib for a long time and it's amazing: thanks!

I found some limits in sorting and filtering in populated content.

If I've understand well, it's impossible to sort by populated fields:
?populate=a,b,c&sort=a.f1,b.f2,c.f1

or to filter by populated fields:
?populate=a,b,c&a.f1=value&b.f2=value2

Is it right?
Do you have any suggestion about this need?

At the moment I tried to use "advanced filter" like
filter={"$or":[{"key1":"value1"},{"key2":"value2"}]}
but it's not so comfortable.

Thanks for any help ✌️

@loris
Copy link
Owner

loris commented Mar 27, 2020

Hi @Pablo-Galbusera sorry for the late reply,

Indeed, this is not working out of the box in the library, because the feature you are looking for is a Mongoose feature whereas the library has been made to simply convert a query string to an object that can be useful for any mongo clients.

Having said that, mongoose is really popular. @jaysalvat already did a PR to handle "projection" (have a look at the mergeProjectionAndPopulation method). I am not a user of the "populate" feature from mongoose, but I think a PR which improve the mergeProjectionAndPopulation method to something more powerful like handleMongoosePopulation, which would extract not only projection, but also sort and filter from the top-level object and put them in the right place in the population object would be nice.

If you don't want to do a PR, this is something you can do on your side:
Right now the library will return something like this:

> aqp("populate=a,b,c&foo=bar&a.f1=value&b.f2=value2");
{
  population: [ { path: 'a' }, { path: 'b' }, { path: 'c' } ],
  filter: { foo: 'bar', 'a.f1': 'value', 'b.f2': 'value2' }
}

and your code will need to update the result, to something like

{
  population: [ { path: 'a', match: { f1: 'value' } }, { path: 'b', match: { f2: 'value2' } }, { path: 'c' } ],
  filter: { foo: 'bar' }
}

ping @jaysalvat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants