使用PHP-Lib在MongoDB中生成序列

I am porting an old PHP application from the legacy Mongo DB code to the current mongoDB library version. For invoice generation, I require a sequence. Previously I used:

db.system.js.save(
{
    _id: "getNextSequence",
    value: function (client, id) {
       var ret = db.counter.findAndModify(
            {
                query: {client: client, id: id},
                update: {$inc: {seq: 1}},
                new: true
            }
        );

        return ret.seq;
    }
});

And from PHP side:

$getNextSequence = new MongoCode("getNextSequence('$client', 'invoices')");
$ret = $this->db->execute($getNextSequence);
$nextId = $ret['retval'];

Unfortunately this is no longer possible as the MongoCode class is no longer around. And frankly, the documentation on MongoDB PHP Library is useless.

Question: How can I either

a) call this Javascript from PHP using the new MongoDB PHP Library or

b) find a different way to increase the sequence and fetch the updated number at the same time

$collection = $this->db->counter;
$ret = $collection->findOneAndUpdate(
    array("client" => $client, "id" => "invoices"),
    array('$inc' => array("seq" => 1)),
    array("new" => true, "upsert" => true)
);
$nextId = $ret->seq;

Works nicely, except for the very first insert, in which case MongoDB provides an empty $id. Improvement ideas are welcome.