This question already has an answer here:
Here I have created a collection with a single document
db.getCollection('example').insert({"example":1});
On the mongo shell command, I am able to get the output using the mongo cursor map command. However, I am struggling to find the equivalent PHP driver implementation.PHP Mongo Cursor. Any alternatives?
db.getCollection('example').find({"example":1},{"_id":1}).map(function(doc) {
return {'id': doc._id.str }
})
</div>
You may find Doctrine implementation of ArrayCollection or similar of that to be useful with their own iterators map methods doctrine/common lib or doctrine/collection as well. In your particular case i'd recommend to implement simple generator with callable argument (if you need to left functionality of Cursor loading) or just use basic array_map function around result of iterator_to_array with Cursor argument. Cheers.
UPD: Here simple generator example:
php function map_traversable(callable $mapper, \Traversable $iterator) { foreach($iterator as $val) { yield $mapper($val); } }
In that case you will walk throught the data once.