Auto increment findOneAndUpdate don't work PHP

Hello,

I try to make a function that increases counters. It increases the counter but after that, I can reload the page and it’s not increasing. When I wait for a few minutes and reload again it increases the counter again.

I’m totally confused. why this weird behavior?

$nr = autoinc('oid');

var_dump($nr);

function autoinc($sequence) {

global $db;

$nr = $db->counters->findOneAndUpdate(

    ['_id' => $sequence],
    ['$inc' => ['sequence' => 1]],
    [
        'projection' => ['sequence' => 1],
        'returnDocument' => MongoDB\Operation\FindOneAndUpdate::RETURN_DOCUMENT_AFTER
    ]
);

return $nr->sequence;

}

Try

 [
        'projection' => ['sequence' => 1],
        'returnNewDocument' => true
]

Thank you. After I noticed this isn’t working but calling the PHP from cli. I noticed the webserver send me a cached response. Stupid me :wink: Sorry for wasting your time!

1 Like