# Filter nested documents with collectionlink -\> \_id fields,

**URL:** <https://discourse.getcockpit.com/t/filter-nested-documents-with-collectionlink-id-fields/2066>\
**Category:** Support\
**Created:** [September 7, 2021, 8:44am UTC](https://discourse.getcockpit.com/t/filter-nested-documents-with-collectionlink-id-fields/2066 "2021-09-07T08:44:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![drascom](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/drascom/32/718_2.png) [@drascom](https://discourse.getcockpit.com/u/drascom)\
**Post date:** [September 7, 2021, 8:44am UTC](https://discourse.getcockpit.com/t/filter-nested-documents-with-collectionlink-id-fields/2066/1 "2021-09-07T08:44:13Z")

</div>

hi there i have post tag relation collection and using it to solve many-to-many relation.  
i’m trying to filter tags which is related exact post but i cant figure it out to filter by collection link \_id  
this is a snippet to show what i want to achieve

```auto
if (COCKPIT_API_REQUEST) {
    $app->on('collections.find.after.posts', function ($name, &$entries) use ($app) {
        foreach ($entries as &$entry) {
            $id = $entry["_id"];
            $tags = $this->module('collections')->find('tags',['_id' => $id]);//problem on filter
            if (!empty($tags)) {
            // foreach ($tags as &$tag) {
                // $entry["linked"][]=$tag;
                // }
            } else {
                $entry["linked"][]="";
            }
        }
    });
}

```

related field is post and i need to filter post[\_id] field

i tried  
[‘post’][’\_id] // no result  
[‘post.\_id’ =\> $id] //all results no filter  
[[‘account.\_id’] =\> $id] //Illegal offset type

Thanks in advance

---

<div class="post-metadata">

**Author:** ![drascom](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/drascom/32/718_2.png) [@drascom](https://discourse.getcockpit.com/u/drascom)\
**Post date:** [September 7, 2021, 12:02pm UTC](https://discourse.getcockpit.com/t/filter-nested-documents-with-collectionlink-id-fields/2066/3 "2021-09-07T12:02:16Z")

</div>

i found solution posting here for someone needs  
My goal was getting all posts with related tags included in this request (prevent from makng second request)  
so 1st collection is

> post

2nd collection

> tags

3rd collection is

> post-tags-relation

with snippets below i’m getting post entries and getting related tags for each post and adding them to the post entry.to filter relation collection i need to filter “\_id” which is located

```auto
{
  "post": {
    "_id": "6135b668386234474e000304", <--- here
    "link": "posts",
    "display": "aaaa"
  },
  "tag": {
    "_id": "61352dda346364830300027e",
    "link": "tags",
    "display": "IZMIR"
  },
  "_by": null,
  "_modified": 1631010110,
  "_created": 1630912806,
  "_id": "6135c126633863531c0001a6",
  "test": "1",
  "_mby": "5fb17db2316366342100001f"
}

```

and to filter those records

```auto
if (COCKPIT_API_REQUEST) {
    $app->on('collections.find.after.posts', function ($name, &$entries) use ($app) {
        foreach ($entries as &$entry) {
            $id = $entry["_id"];
            $tags = $this->module('collections')->find('post-tags-relation', ["filter"=>['tag._id' => $id]]);
            if (!empty($tags)) {
                foreach ($tags as &$tag) {
                    $entry["linked"][]=$tag["tag"];
                }
            } else {
                $entry["linked"][]="";
            }
        }
    });
}

```

I hope this helps someone needed who like i, desperately digging forum and google…
