# Filtering on a collectionlink

**URL:** <https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197>\
**Category:** Support\
**Created:** [August 13, 2018, 12:10pm UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197 "2018-08-13T12:10:38Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sephen](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Sephen](https://discourse.getcockpit.com/u/Sephen)\
**Post date:** [August 13, 2018, 12:10pm UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/1 "2018-08-13T12:10:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Sephen](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Sephen](https://discourse.getcockpit.com/u/Sephen)\
**Post date:** [August 23, 2018, 7:18am UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/2 "2018-08-23T07:18:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pauloamgomes](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/pauloamgomes/32/48_2.png) [@pauloamgomes](https://discourse.getcockpit.com/u/pauloamgomes)\
**Post date:** [August 23, 2018, 8:41am UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/3 "2018-08-23T08:41:35Z")

</div>

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
}
```

---

<div class="post-metadata">

**Author:** ![adrigm](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/adrigm/32/81_2.png) [@adrigm](https://discourse.getcockpit.com/u/adrigm)\
**Post date:** [September 4, 2018, 6:39pm UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/4 "2018-09-04T18:39:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pauloamgomes](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/pauloamgomes/32/48_2.png) [@pauloamgomes](https://discourse.getcockpit.com/u/pauloamgomes)\
**Post date:** [September 4, 2018, 9:32pm UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/5 "2018-09-04T21:32:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adrigm](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/adrigm/32/81_2.png) [@adrigm](https://discourse.getcockpit.com/u/adrigm)\
**Post date:** [September 4, 2018, 9:47pm UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/6 "2018-09-04T21:47:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sephen](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Sephen](https://discourse.getcockpit.com/u/Sephen)\
**Post date:** [September 13, 2018, 8:55am UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/7 "2018-09-13T08:55:56Z")

</div>

@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! 😃

---

<div class="post-metadata">

**Author:** ![Sephen](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Sephen](https://discourse.getcockpit.com/u/Sephen)\
**Post date:** [September 19, 2018, 11:26am UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/8 "2018-09-19T11:26:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![serjoscha87](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/serjoscha87/32/151_2.png) [@serjoscha87](https://discourse.getcockpit.com/u/serjoscha87)\
**Post date:** [September 19, 2018, 4:14pm UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/9 "2018-09-19T16:14:24Z")

</div>

take a look at this [[Help] Problems filtering API content. · Issue #681 · agentejo/cockpit · GitHub](https://github.com/agentejo/cockpit/issues/681#issuecomment-368273914)

> [@](#):
>
> if you’re using mongodb as cockpits data source then this filter query should work:
> 
> `filter[telefono][value]=1111`
> 
> but if you’re using the default MongoLite data source then querying nested values isn’t supported, sorry.

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.

---

<div class="post-metadata">

**Author:** ![TheIpHunter](https://avatars.discourse-cdn.com/v4/letter/t/45deac/32.png) [@TheIpHunter](https://discourse.getcockpit.com/u/TheIpHunter)\
**Post date:** [November 9, 2019, 4:25pm UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/10 "2019-11-09T16:25:38Z")

</div>

Hi,  
I’m also trying to filter via a linked value  
 ![2019-11-09%2017_22_29-Clipboard](https://canada1.discourse-cdn.com/flex030/uploads/getcockpit/original/1X/c17f4de0bfac68c12bdd2ddbd3537f32c58ad502.png)  
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]  
]);

---

<div class="post-metadata">

**Author:** ![raffaelj](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/raffaelj/32/331_2.png) [@raffaelj](https://discourse.getcockpit.com/u/raffaelj)\
**Post date:** [November 9, 2019, 5:53pm UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/11 "2019-11-09T17:53:05Z")

</div>

I guess, you use SQLite

> [@Filtering on a collectionlink](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/9):
>
> take a look at this [https://github.com/agentejo/cockpit/issues/681#issuecomment-368273914](https://github.com/agentejo/cockpit/issues/681#issuecomment-368273914) 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.

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

```php
$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](https://github.com/raffaelj/cockpit_SelectRequestOptions) 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.

---

<div class="post-metadata">

**Author:** ![TheIpHunter](https://avatars.discourse-cdn.com/v4/letter/t/45deac/32.png) [@TheIpHunter](https://discourse.getcockpit.com/u/TheIpHunter)\
**Post date:** [November 10, 2019, 9:12am UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/12 "2019-11-10T09:12:27Z")

</div>

category.0.display works perfect for my  
Thanks

---

<div class="post-metadata">

**Author:** ![easysoft-mobility](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/easysoft-mobility/32/500_2.png) [@easysoft-mobility](https://discourse.getcockpit.com/u/easysoft-mobility)\
**Post date:** [February 28, 2020, 4:06pm UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/14 "2020-02-28T16:06:29Z")

</div>

Worked for me too !  
Thanks a lot !

---

<div class="post-metadata">

**Author:** ![linhous](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/linhous/32/900_2.png) [@linhous](https://discourse.getcockpit.com/u/linhous)\
**Post date:** [September 7, 2021, 9:23pm UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/15 "2021-09-07T21:23:38Z")

</div>

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:

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

```

But I was not successful…

---

<div class="post-metadata">

**Author:** ![abernh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/abernh/32/859_2.png) [@abernh](https://discourse.getcockpit.com/u/abernh)\
**Post date:** [September 8, 2021, 7:43am UTC](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/16 "2021-09-08T07:43:37Z")

</div>

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

> [@Filtering on a collectionlink](https://discourse.getcockpit.com/t/filtering-on-a-collectionlink/197/9):
>
> take a look at this [https://github.com/agentejo/cockpit/issues/681#issuecomment-368273914](https://github.com/agentejo/cockpit/issues/681#issuecomment-368273914) 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.

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

```auto
/api/collection/events/?categories=Adventsfenster,Sonnentanz

```

```auto
// 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_
