Bostjan Laba
Bostjan Laba

Reputation: 23

MongoDB 3.6 with PHP 7.4: unknown operator $text

 $connection = new MongoDB\Driver\Manager("mongodb://localhost:27017");
  $db = "db";
  $coll = "calls";
  $filter = [];
  $options= [];
  $coll = "calls";
  $Client= "linux";
  $query = new MongoDB\Driver\Command(['count' => $coll, 'query' => ['Client' => ['$text' => ['$search' => $Client]]]]);
  $result = $connection->executeCommand($db,$query);
  $res = current($result->toArray());
  $countAll = $res->n;
  echo ("Total linux clients \"Unknown\": " . $countAll . "\n");

Why does this script throw an error that $text operator is unknown? if I use $gt or $eq on some other field but in the same syntax, they work. Just $text won't.

The goal is to count documents that have Client field with contents "linux" (e.g linux-1, linux0.7 etc).

Upvotes: 1

Views: 1728

Answers (1)

thammada.ts
thammada.ts

Reputation: 5245

$text performs a text search on the content of the fields indexed with a text index.

You can not use $text on a specific field. If you use $text, MongoDB will search through all fields that is text indexed.

So your command shouldn't include Client

['count' => $coll, 'query' => ['$text' => ['$search' => $Client]]]

Upvotes: 1

Related Questions