3

is there any way to create a new sequence number from a default starting number using "findAndModify" in jenssegers laravel 5.1 and mongoDB 2.6 package or at-least in mongo 2.6?

I have a counter collection and I am placing all my auto increment counters in that. And now there is a need for me to reset the sequence number to 100000 every financial year start.

For Example

This year (15-16) the counter is started from 100,000 and incremented 12K records and reached to 112,000.

Now in the next year the counter should start from 100,000. For auto increment I have used the below code.

public function getNextSequence($name) {
    $retval = DB::getCollection('my_counts')->findAndModify(
            array('_id' => $name), array('$inc' => array('seq' => 1)), null, 
                  array('new' => true, 'upsert' => true)
    );
    return (int) $retval['seq'];
}

and the data in collection like below.

enter image description here

so in the image above "grants_15-16" is current year's sequence. Now when 16-17 comes I will pass as "grants_16-17". So, that value doesn't existed in the collection and mongo will create one. But the problem here is, the "seq" is starting from the "1". But I want it to be start from "100000".

So I have changed the code like below.

public function getNextSequence($name) {
    $retval = DB::getCollection('test_counters')->findAndModify(
            array('_id' => $name), array('$setOnInsert' => array('seq' => 100000), 
                  '$inc' => array('seq' => 1)), 
                  null, array('new' => true, 'upsert' => true)
    );
    return (int) $retval['seq'];
}

but it returns the error like below.

enter image description here

I have searched for the solution, but no use. Please help with this.

Thanks in Advance.

Kiran
  • 1,177
  • 4
  • 18
  • 35

0 Answers0