title: mongo PHP app error: Fatal error: Uncaught exception 'MongoException' with message ''.' not allowed in key
body: <p>I have the following PHP array: </p>
  
  <pre><code>array (size=9)
    'script_desc' =&gt; string 'Test Script' (length=21)
    'script_date' =&gt; string 'May 11 2016 15:40:48' (length=20)
    'log_date' =&gt; string 'May 12 2016 09:17:58' (length=20)
    'name' =&gt; string 'test name' (length=2)
    'type' =&gt; string 'location_status' (length=15)
    'status' =&gt; boolean false
    'test.server.1' =&gt; 
      array (size=1)
        'packages' =&gt; 
          array (size=2)
            'package1' =&gt; 
              array (size=4)
                'package_name' =&gt; string 'package1' (length=10)
                'current_version' =&gt; string 'package1-1.4.26-r1' (length=20)
                'latest_version' =&gt; string '1.4.26-r1' (length=9)
                'package_status' =&gt; string '=' (length=1)
            'package2' =&gt; 
              array (size=4)
                'package_name' =&gt; string 'package2' (length=14)
                'current_version' =&gt; string 'package2-0.31.1-r1' (length=24)
                'latest_version' =&gt; string '0.31.1-r2' (length=9)
                'package_status' =&gt; string '&lt;' (length=1)
    'test.server.2' =&gt; 
      array (size=1)
        'packages' =&gt; 
          array (size=2)
            'package1' =&gt; 
              array (size=4)
                'package_name' =&gt; string 'package1' (length=16)
                'current_version' =&gt; string 'package1-0.35.1-r0' (length=26)
                'latest_version' =&gt; string '0.35.1-r0' (length=9)
                'package_status' =&gt; string '=' (length=1)
            'package3' =&gt; 
              array (size=4)
                'package_name' =&gt; string 'package3' (length=3)
                'current_version' =&gt; string 'package3-5.3.2-r0' (length=33)
                'latest_version' =&gt; string '5.3.2-r0' (length=8)
                'package_status' =&gt; string '=' (length=1)
    '_id' =&gt; float 5
  </code></pre>
  
  <p>Here's the code that I use to try to insert this array into my mongo database: </p>
  
  <pre><code> 68 function add_history_record($location)
   69 {
   70         $m = new MongoClient("mongodb://10.1.1.1:27017");
   71         $db = $m-&gt;mymongodb;
   72         $collection = $db-&gt;mycollection;
   73         $location['_id'] = getNextSequence("locationid");
   74         $cursor = $collection-&gt;insert($location);
   75 var_dump($cursor);
   76 }
  </code></pre>
  
  <p>The error message I'm getting is this: </p>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'MongoException' with message ''.' not
    allowed in key: test.server.1' in
    /var/www/html/mongotestapp/inventory.php on
    line 74</p>
  </blockquote>
  
  <p><strong>What I've tried So Far:</strong> </p>
  
  <p>To prove to myself that I can have keys with "." in it, I used robomongo to add the following document: </p>
  
  <pre><code>{
      "_id" : ObjectId("573483ae3747106e60a087f9"),
      "test.server.1.1" : 123
  }
  </code></pre>
  
  <p>It saved the document in my collection no problems. 
  I'm not sure what I'm missing in my PHP code... 
  Any suggestions? </p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>I've changed my code to include the following: </p>
  
  <pre><code> 68 function add_playbook_history_record($location)
   69 {
   70         $m = new MongoClient("mongodb://10.1.1.1:27017");
   71         $db = $m-&gt;phonesys;
   72         $collection = $db-&gt;inventory;
   73         $location['_id'] = getNextSequence("locationid");
   74         printf($location['_id']); 
   75         echo "&lt;pre&gt;".json_encode($location)."&lt;/pre&gt;";
   76         $cursor = $collection-&gt;insert($location, array("w"=&gt;1));
   77 
   78 //      $cursor = $collection-&gt;insert($location, array("w" =&gt; 0,"j"=&gt;true));
   79 }
  </code></pre>
  
  <p>I can see that the system is generating a legit location id. 
  I also took the json formatted output and tested in jsonlint to make sure it's properly formatted etc. 
  Lastly, I started playing with the write options.  When I enable line 78, the system doesn't return any error messages, but the records are not added to the database.  But when I set w=1, it fails with the same error message about the key.<br>
  I've been playing around with the different options and reading this: <a href="http://php.net/manual/en/mongo.writeconcerns.php" rel="nofollow">http://php.net/manual/en/mongo.writeconcerns.php</a>
  but if you have any tips I'm all ears. </p>
  
  <p>I don't have any shards or other servers. Just one main mongodb server running right now. </p>
  
  <p>Thanks. </p>