You are here

public function Tools::find in MongoDB 8.2

Command callback for mongodb:mdbf.

Parameters

string $alias: The alias of the database in which to perform the query.

string $collection: The name of the collection in which to find.

string $selector: The selector to apply to the query.

Return value

array The query results.

File

modules/mongodb/src/Install/Tools.php, line 72

Class

Tools
Class MongoDbCommands provides the Drush commands for the mongodb module.

Namespace

Drupal\mongodb\Install

Code

public function find(string $alias, string $collection, string $selector = '{}') : array {

  /** @var \MongoDB\Database $database */
  $database = $this->dbFactory
    ->get($alias);
  $jsonSelector = json_decode($selector);
  if ($jsonSelector === NULL) {
    throw new InvalidArgumentException("Your JSON selector could not be decoded. Here is how PHP received it: " . var_export($selector, TRUE));
  }
  $docs1 = $database
    ->selectCollection($collection)
    ->find($jsonSelector, [
    'typeMap' => [
      'root' => 'array',
      'document' => 'array',
      'array' => 'array',
    ],
  ]);
  $docs2 = [];

  // Convert objects in result set to hashes.
  foreach ($docs1 as $doc1) {
    $docs2[] = json_decode(json_encode($doc1), TRUE);
  }
  return $docs2;
}