0

i have one problem. When i try to execute this query, query give me this error "invalid operator '$addToSet'".

        $qb = $this->createAggregationBuilder();
        $qb
            ->match()
            ->field('categories')->in($categories)
            ->field('hidden')->field('hidden')->notEqual(true)
            ->unwind('$type')
            ->unwind('$variants')
            ->unwind('$variants.subvariants')
            ->unwind('$variants.subvariants.params')
            ->group()
            ->field('id')->expression('$type')
            ->field('param')->addToSet(
                $qb
                    ->expr()
                    ->field('id')->expression('$variants.subvariants.params.param')
                    ->field('values')->addToSet('$variants.subvariants.params.value')
            );

        $res_params = $qb->execute()->toArray();

Does anyone know how to execute this query correctly?

I need result like this

[
    {
        "_id": {
            "$ref": "Type",
            "$id": "f564ece4fe4b4cdd9f1ccd8f092a98f9",
            "$db": "skibike"
        },
        "param": [
            {
                "_id": {
                    "$ref": "Param",
                    "$id": "10f67225cfbe4c0e962828f32ea0fc25",
                    "$db": "skibike"
                },
                'values': [1, 2, 3]
            },
            {
                "_id": {
                    "$ref": "Param",
                    "$id": "5ee15b7cc9404ceab9eb41f48e8622f5",
                    "$db": "skibike"
                },
                'values': [5, 4]
            }
        ]
    }
]

Thank you

Venon
  • 3
  • 4
  • What does the documentation say? – D. SM Sep 12 '20 at 00:52
  • What do you mean? Which documentation? – Venon Sep 14 '20 at 09:15
  • The documentation for the operations you are using like addtoset. – D. SM Sep 14 '20 at 09:21
  • This. **The addToSet strategy uses MongoDB's $addToSet operator to insert elements into the array. This strategy is useful for ensuring that duplicate values will not be inserted into the collection. Like the pushAll strategy, elements are inserted in a separate query after removing deleted elements.** [link](https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/2.1/reference/storage-strategies.html) – Venon Sep 14 '20 at 09:35

0 Answers0