Search code examples
mongodbyiiyii2yii2-advanced-appyii-extensions

Yii2 convert MongoDB query a Yii query


I have use this query command in terminal on mongo and it's working with scor sort

db.stores.createIndex( { name: "text", description: "text" } )

db.articles.find( { $text: { $search: "coffee" } } )

this query is working on mongo on my terminal I did :

$collection = Yii::$app->mongodb->getCollection('mydata');
        $result = $collection->find([
            [
                '$text' => [
                    '$search' => $key,
                ]
            ]
        ]);

The query is working good on mongo on terminal I copy it from : https://docs.mongodb.com/manual/reference/operator/query/text/

The error I get :

TypeError
strtoupper(): Argument #1 ($string) must be of type string, array given

Solution

  • I've not used mongodb with Yii myself but I think you've gone one level too deep with the array.

    $collection = Yii::$app->mongodb->getCollection('mydata');
    $result = $collection->find([
      '$text' => [
        '$search' => $key,
      ]
    ]);