# CockpitCore: query on set of multiple collection Links

**URL:** <https://discourse.getcockpit.com/t/cockpitcore-query-on-set-of-multiple-collection-links/2434>\
**Category:** Cockpit v2\
**Created:** [July 27, 2022, 7:44am UTC](https://discourse.getcockpit.com/t/cockpitcore-query-on-set-of-multiple-collection-links/2434 "2022-07-27T07:44:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbeuria](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@jbeuria](https://discourse.getcockpit.com/u/jbeuria)\
**Post date:** [July 27, 2022, 7:44am UTC](https://discourse.getcockpit.com/t/cockpitcore-query-on-set-of-multiple-collection-links/2434/1 "2022-07-27T07:44:43Z")

</div>

Suppose I have model with field of type ‘set’ with contentlinks and each content link has multiple entries as highlighted below.

```auto
  "target": [
    {
      "A": [
        {
          "_model": "Type-A",
          "_id": "e34891a266653524790000b5"
        }
      ],
     "B": [
        {
          "_model": "Type-B",
          "_id": "b34891a278953524791111c9"
        }
      ],
    }
  ],

```

How do I make a custom query? I am using

```auto
        $options=array(
            'filter' => [
                'target.A._id' =>$targetID,
            ], "populate"=> 1);

```

However, query does not return any entries. Instead, following syntax works which is of no use since I cannot fix the index.

```auto
        $options=array(
            'filter' => [
                'target.A.0._id' =>$targetID,
            ], "populate"=> 1);

```

Could anyone suggest something?

---

<div class="post-metadata">

**Author:** ![jbeuria](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@jbeuria](https://discourse.getcockpit.com/u/jbeuria)\
**Post date:** [July 27, 2022, 10:39am UTC](https://discourse.getcockpit.com/t/cockpitcore-query-on-set-of-multiple-collection-links/2434/2 "2022-07-27T10:39:18Z")

</div>

I think I found the solution. Someone please validate.

```auto

$var1=$targetID; // some local variables

$options=array(
            'filter' => [
                '$where' => function($doc) use ($var1) 
                 {
                     // here $doc is the full JSON entry.
                     return a boolean value depending on the condition.
                 }
            ]);

```

---

<div class="post-metadata">

**Author:** ![artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/artur/32/4_2.png) [@artur](https://discourse.getcockpit.com/u/artur)\
**Post date:** [July 27, 2022, 10:43am UTC](https://discourse.getcockpit.com/t/cockpitcore-query-on-set-of-multiple-collection-links/2434/3 "2022-07-27T10:43:29Z")

</div>

do you use mongodb onr mongolite (sqlite) as datasource?

---

<div class="post-metadata">

**Author:** ![jbeuria](https://avatars.discourse-cdn.com/v4/letter/j/b77776/32.png) [@jbeuria](https://discourse.getcockpit.com/u/jbeuria)\
**Post date:** [July 27, 2022, 10:46am UTC](https://discourse.getcockpit.com/t/cockpitcore-query-on-set-of-multiple-collection-links/2434/4 "2022-07-27T10:46:31Z")

</div>

I am using Mongolite (sqlite).

---

<div class="post-metadata">

**Author:** ![artur](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.getcockpit.com/artur/32/4_2.png) [@artur](https://discourse.getcockpit.com/u/artur)\
**Post date:** [July 27, 2022, 10:53am UTC](https://discourse.getcockpit.com/t/cockpitcore-query-on-set-of-multiple-collection-links/2434/5 "2022-07-27T10:53:27Z")

</div>

🚨 NOT TESTED 🚨

```php
$targetID = 'xyz'; // some local variables

$options = [
    'filter' => [
        '$where' => function($doc) use ($targetID) {
            return count(array_filter($doc['target']['A'] ?? [], fn($v) => $v['_id'] == $targetID)) > 0;
        }
    ]
];

```
