2

I use this MongoDb driver in my application. I like it, because it works well. I can run simple select and insert statements, like:

$em = new MongoDB\Driver\Manager("mongodb://localhost:27017/testdb");
$query = new Query(['name' => 'John']);
$res = $em->executeQuery('users', $query);

But the problem is, I can not find even a single example on making aggregations. PHP documentation does not say a word about this. While, MongoDb documentation seems to use another library:

$collection = (new MongoDB\Client)->test->restaurants;

$cursor = $collection->find([
    'name' => new MongoDB\BSON\Regex('^' . preg_quote('(Library)')),
]);

It seems like an example from another library, because MongoDb driver does not have this MongoDB\Client class. There is such a class, but in a deprecated library. So, what is the right way to make aggregations in PHP, using modern MongoDb driver?

1 Answer 1

1

This extension provides a minimal API for core driver functionality: commands, queries, writes, connection management, and BSON serialization.

https://www.php.net/manual/en/set.mongodb.php

To execute aggregate command, use MongoDB\Driver\Command

{
  aggregate: "<collection>" || 1,
  pipeline: [ <stage>, <...> ],
  explain: <boolean>,
  allowDiskUse: <boolean>,
  cursor: <document>,
  maxTimeMS: <int>,
  bypassDocumentValidation: <boolean>,
  readConcern: <document>,
  collation: <document>,
  hint: <string or document>,
  comment: <string>,
  writeConcern: <document>
}

https://docs.mongodb.com/manual/reference/command/aggregate/#syntax

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.