I'm trying to adapt the answer(Map Reduce Approach) provided in this question into php:
Does MongoDB's $in clause guarantee order
Here's what I have done, I get no errors, but I receive no results:
$items = array('523fe7e00000000000000000', '5230e7e00000000000000000', '5240e1e00000000000000000');
$items_converted = array();
foreach($items as $an_item){
$items_converted[] = new MongoID($an_item);
}
$map = new MongoCode('
function () {
var order = inputs.indexOf(this._id);
emit( order, { doc: this } );
}
');
$finalize = new MongoCode('
function (key, value) {
return value.doc;
}
');
$results = $db->command(array(
'mapreduce' => 'template_items',
'map' => $map,
'out' => array('inline' => 1),
'query' => array('_id' => array('$in' => $items_converted)),
'scope' => array('inputs' => $items_converted),
'finalize' => $finalize
));
foreach($results as $row){
echo $row['title'];
}
The main goal of this is to sort the results in the exact order as the array containing the IDs. I can get it to work in command line, just having trouble adapting it into php.