Skip to content

Instantly share code, notes, and snippets.

@debojitkakoti
Last active October 27, 2020 06:31
Show Gist options
  • Select an option

  • Save debojitkakoti/9565409 to your computer and use it in GitHub Desktop.

Select an option

Save debojitkakoti/9565409 to your computer and use it in GitHub Desktop.

Revisions

  1. debojitkakoti revised this gist Apr 1, 2014. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions seq_mongo_auto.php
    Original file line number Diff line number Diff line change
    @@ -3,14 +3,14 @@

    // select a database
    $db = $m->seq;

    // select a collection (analogous to a relational database's table)
    $collection = $db->counters;
    $user_collection = $db->user;



    /**********Function to auto increment seq************/
    /******************Function to auto increment seq******************/
    function getNextSequence($name){
    global $collection;

    @@ -24,7 +24,8 @@ function getNextSequence($name){
    );
    return $retval['seq'];
    }


    /********************Example Usage**********************************/
    $db_array=array('_id' => getNextSequence("userid"), 'name' => 'debojit');

    $user_collection->insert($db_array);
  2. debojitkakoti created this gist Mar 15, 2014.
    30 changes: 30 additions & 0 deletions seq_mongo_auto.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    <?php
    $m = new MongoClient();

    // select a database
    $db = $m->seq;

    // select a collection (analogous to a relational database's table)
    $collection = $db->counters;
    $user_collection = $db->user;



    /**********Function to auto increment seq************/
    function getNextSequence($name){
    global $collection;

    $retval = $collection->findAndModify(
    array('_id' => $name),
    array('$inc' => array("seq" => 1)),
    null,
    array(
    "new" => true,
    )
    );
    return $retval['seq'];
    }

    $db_array=array('_id' => getNextSequence("userid"), 'name' => 'debojit');

    $user_collection->insert($db_array);