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;
}