title: MongoDB - how to insert record using autoincrement functionality
body: <p>I'm following the documentation found here 
  <a href="https://docs.mongodb.com/v3.0/tutorial/create-an-auto-incrementing-field/" rel="nofollow noreferrer">https://docs.mongodb.com/v3.0/tutorial/create-an-auto-incrementing-field/</a></p>
  
  <p>I have two counters, one called locationid and the other called playhistid. 
  This is what they look like: </p>
  
  <pre><code>{
      "_id" : "locationid",
      "seq" : 0
  }
  
  {
      "_id" : "playhistid",
      "seq" : 0
  }
  </code></pre>
  
  <p>I've also created the following function: </p>
  
  <pre><code>function getNextSequence(name) {
     var ret = db.counters.findAndModify(
            {
              query: { _id: name },
              update: { $inc: { seq: 1 } },
              new: true
            }
     );
  </code></pre>
  
  <p>Now I need a way to call <code>getNextSequence("locationid")</code> from PHP.<br>
  This is what my code looks like so far: </p>
  
  <pre><code>function add_playbook_history_record($location)
  {
          $m = new MongoClient("mongodb://10.1.1.111:27017");
          $db = $m-&gt;testdb;
          $collection = $db-&gt;testcollection;
          $cursor = $collection-&gt;insert($location);
  }
  </code></pre>
  
  <p>As you can see, I'm not calling the getNextSequence() function because I don't know how to do this in.  I found a similar question on this site:
  <a href="https://stackoverflow.com/questions/9921904/how-to-create-auto-increment-field-in-mongodb-and-php">How to create auto increment field</a></p>
  
  <p>But... they don't actually show how to do this in PHP. They just show the material from the mongo docs.</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>What I'm thinking of is trying to find a way to run the getNextSequence() using the db->command() method... and then adding a new item to my $location array like this: </p>
  
  <pre><code>$location["_id"] = value_from_db_command; 
  </code></pre>