api-query-params icon indicating copy to clipboard operation
api-query-params copied to clipboard

Sort and filter in population

Open Pablo-Galbusera opened this issue 5 years ago • 1 comments

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 ✌️

Pablo-Galbusera avatar Feb 26 '20 12:02 Pablo-Galbusera

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

loris avatar Mar 27 '20 10:03 loris