CockpitCore: query on set of multiple collection Links

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

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

How do I make a custom query? I am using

        $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.

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

Could anyone suggest something?

I think I found the solution. Someone please validate.


$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.
                 }
            ]);

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

I am using Mongolite (sqlite).

:rotating_light: NOT TESTED :rotating_light:

$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;
        }
    ]
];
1 Like