2

I'm fairly new to Mongo and I have what I thought was a simple question. How do I do MapReduce with PHP and the non legacy MongoDB driver http://php.net/manual/en/set.mongodb.php or the higher level package mongodb/mongodb found at https://packagist.org/packages/mongodb/mongodb?

  • Every example I've seen seems to use the legacy driver (http://php.net/manual/en/book.mongo.php). They all use the MongoCode object, which doesn't exist in mongodb.php. It exists in mongo.php (the legacy driver). When I try and use it, it will say that "Class 'MongoCode' not found".

My code looks something like:

$function = "function() { emit(this); }";
$map = new \MongoCode($function);

$command = $db->command([
    "mapreduce" => "db.archiveData",
    "map" => $map,
    "query" => $query,
    "out" => "data"
]);

So is it that:

  • MapReduce is not supported with mongodb/mongodb. Or maybe it is not supported yet, but will be?
  • I have to use the legacy driver for MapReduce?
  • I have to figure out a way to call db.collection.mapReduce via JavaScript on the server?
  • I have to use the Aggregation Pipeline (https://docs.mongodb.com/manual/aggregation/) to do map reduce type of actions? But that feels much more limited.

What am I missing?

3
  • Have a check on this (if you haven't already): github.com/mongodb/mongo-php-library/issues/379 (Jun 19 2017), it says something about MongoDB\Database::command() and that MapReduce is not yet implemented. Commented Sep 28, 2017 at 18:36
  • Thanks @Parziphal. Good find. Not what I was hoping for, but it doesn't sound like the new library supports it. I also just discovered that I cannot use the legacy driver because it doesn't look like it's supported on PHP 7.0 (docs.mongodb.com/ecosystem/drivers/php/#compatibility) which we are using. Commented Sep 29, 2017 at 17:03
  • Yeah, I had a very similar problem long time ago, PHP 7.0, new mongo drivers that are incomplete... how we fixed it?... I don't remember, but around that time we started using Facebook's Parse so we stopped touching MongoDB directly and I started to use Node more often. So actually I never found a solution. Commented Sep 29, 2017 at 19:38

1 Answer 1

2

So I now have clarity on where things are at.

Example is here as well:

$database = (new MongoDB\Client)->selectDatabase('db_name');
$cursor = $database->command([
    'mapReduce' => 'collection_name',
    'map' => new MongoDB\BSON\Javascript('...'),
    'reduce' => new MongoDB\BSON\Javascript('...'),
    'out' => 'output_collection_name',
]);

$resultDocument = $cursor->toArray()[0];

You can also use MapReduce via Doctrine (http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/map-reduce.html), but that is using legacy and a shim. So probably not a good choice for a new project.

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.