Filtering on a collectionlink

Hi,

How to filter on a collection link?

I tried:
{ “filter”: { “categories”: { “$elemMatch”: { “name”: “images” } } }, “populate”: 1 }
{ “filter”: { “categories”: { “name”: “images” } }, “populate”: 1 }
{ “filter”: { “categories”: { “_id”: “5a9506efec401doc1908157538” } }, “populate”: 1 }

But without any results

I get:
{
“error”: true,
“message”: “Condition not valid … Use name for custom operations”
}
as a reply

Thanks in advance

Hi,

Apparently i’m the only one with this issue.
Can someone give me an example of how to do this. I just can’t get it to work.

Thanks in advance

Hi @Sephen, believe you are not using properly the filter attribute, so for what I understood, you have a collection with a collectionlink field, and you want to filter your entries using that field.

It seems you can only use fields that are in the actual response (not the fields that are part of the linked collection), so if you have defined a link to categories like below:

{
  "link": "categories",
  "display": "name",
  "multiple": false,
  "limit": false
}

when fetching without a filter you’ll have something like:

[
    {
        "name": "link1",
        "categories": {
            "_id": "5aca7ae7db780doc1764785784",
            "link": "categories",
            "display": "Category 1"
        },
(...)

so you can use your filter using id, link or display:

{
  "filter": {
    "link.display": {"$eq": "Category 1"}
   },
  "populate": 0,
  "simple": 1
}

or even simplifying

{
  "filter": { "link.display": "Category 1 },
  "populate": 0,
  "simple": 1
}

Hello,

I try to do this for collections with multiple elements, but I don’t know how to filter elements of an array.

Any ideas?

Thank you

Hey @adrigm, not sure if got it, can you detail it further?

Hello,

It is the same as your previous post, but for collections with several elements.

Imagine tags instead of categories. Cockpit generates an array with tags objects inside.

@pauloamgomes, Sorry for my late reply, but for some reason i did not get notified of your reply. But THANKS! I’m finally able to use the collectionlink! :smiley:

1 Like

The collection-link works fine when using “multiple”: false but when using “multiple”: true i don’t get a response.

i tried things like:

{
  "filter": { "categories": {"display": "Projects"} },
  "populate": 0,
  "simple": 1
}

{
  "filter": { "categories": ["display": "Projects"] },
  "populate": 0,
  "simple": 1
}

{
  "filter": { "categories.display": ["Projects"] },
  "populate": 0,
  "simple": 1
}

But none of those result in a response that return the items

take a look at this [Help] Problems filtering API content. · Issue #681 · agentejo/cockpit · GitHub

guess “multiple” makes the result nested (perhaps?) and mongo lite does not support nested filtering on collection links.

Not quite sure if this helps anyway.

Hi,
I’m also trying to filter via a linked value
2019-11-09%2017_22_29-Clipboard
How does the filter need to be in php:

This doesn’t work
$arrPostList = cockpit(‘collections’)->find(‘events’, [
‘filter’ => [‘category.display’ => [’$eq’ => ‘Adventsfenster’]],
‘sort’ => [‘DateFrom’ => 1]
]);

I guess, you use SQLite

IF you know, that your category is always the first one, this filter should work with category.0.display:

$arrPostList = cockpit('collections')->find('events', [
'filter' => ['category.0.display' => ['$eq' => 'Adventsfenster']],
'sort' => ['DateFrom' => 1]
]);

I don’t know, if there is a better way, because I didn’t use Collection Links for a while now.

Or you could try the SelectRequestOptions Addon instead to select data by custom requests. It doesn’t resolve relations, but if you don’t change your category titles, this could be useful.

3 Likes

category.0.display works perfect for my
Thanks

1 Like

Worked for me too !
Thanks a lot !

What if instead of passing the index, I wanted to search within the categories?

For example, the idea is to pass category.0.display right?

But what if I have 3 categories, and want to know if any of them are equal to “Adventsfenster” and not just index 0?

I tried this:

"filter": {
        "category": {
            "$has" : {
                "display" : "Adventsfenster"
            }
        }
    },

But I was not successful…

Your question is the same, that @raffaelj is answering with the post you posted your question in reply to.

Given the current state in the linked Github issue filtering “nested” (=multiple) collection links is still not supported.

A custom script would have to do the filtering instead

/api/collection/events/?categories=Adventsfenster,Sonnentanz
// config/bootstrap.php

if (COCKPIT_API_REQUEST) {

    $app->on('collections.find.after.events', function ($name, &$entries) use ($app) {
        $catFilter = explode(',', $app->param('categories', ''));
        $entriesFiltered = [];

        foreach ($entries as &$entry) {
            $entryCategories = array_walk($entry['category'], function($cat0){
              return $cat0['display'];
            })

            if(!empty(array_intersect($catFilter, $entryCategories)){
               $entriesFiltered[] = $entry
            }
        }

        $entries = $entriesFiltered;
    });

}

Note: not tested but should work along those lines