Note that there are some explanatory texts on larger screens.

plurals
  1. POAutoincrementing conversion - What am I missing here?
    primarykey
    data
    text
    <p>please refer to the following location <a href="http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/" rel="nofollow">http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/</a></p> <p>In the specified URL (above) there is a sample code for auto increment logic in java script which was converted to php (I have provided the code below) - I am not able to make it error free. could any one correct me where I have gone wrong in the conversion?</p> <pre><code>&lt;?php function getNextSequence($name) { global $db; $ret = $db-&gt;counters-&gt;findAndModify( array( 'query'=&gt;array( '_id'=&gt;name), 'update'=&gt;array( '$inc'=&gt; array('seq'=&gt; 1) ), 'new'=&gt;'true' ) ); return $ret-&gt;seq; } $db = new MongoClient(); $db = $db-&gt;test; $db-&gt;counters-&gt;insert( array( '_id'=&gt;"userid", 'seq'=&gt;0 ) ); $db-&gt;users-&gt;insert( array( '_id'=&gt; getNextSequence("userid"), 'name'=&gt; "Sarah C." ) ); $db-&gt;users-&gt;insert( array( '_id'=&gt; getNextSequence("userid"), 'name'=&gt; "Bob D." ) ); $ret = $db-&gt;users-&gt;find(); print_r($ret); ?&gt; </code></pre> <p>PHP Notice: Use of undefined constant name - assumed 'name' in C:\php\seq.php on line 6 PHP Fatal error: Uncaught exception 'MongoResultException' with message 'need remove or update' in C:\php\seq.php:10 Stack trace: #0 C:\php\seq.php(10): MongoCollection->findAndModify(Array) #1 C:\php\seq.php(25): getNextSequence('userid') #2 {main} thrown in C:\php\seq.php on line 10</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload