mongo PHP app error: Fatal error: Uncaught exception 'MongoException' with message ''.' not allowed in key

人盡茶涼 提交于 2019-12-13 19:14:10

问题


I have the following PHP array:

array (size=9)
  'script_desc' => string 'Test Script' (length=21)
  'script_date' => string 'May 11 2016 15:40:48' (length=20)
  'log_date' => string 'May 12 2016 09:17:58' (length=20)
  'name' => string 'test name' (length=2)
  'type' => string 'location_status' (length=15)
  'status' => boolean false
  'test.server.1' => 
    array (size=1)
      'packages' => 
        array (size=2)
          'package1' => 
            array (size=4)
              'package_name' => string 'package1' (length=10)
              'current_version' => string 'package1-1.4.26-r1' (length=20)
              'latest_version' => string '1.4.26-r1' (length=9)
              'package_status' => string '=' (length=1)
          'package2' => 
            array (size=4)
              'package_name' => string 'package2' (length=14)
              'current_version' => string 'package2-0.31.1-r1' (length=24)
              'latest_version' => string '0.31.1-r2' (length=9)
              'package_status' => string '<' (length=1)
  'test.server.2' => 
    array (size=1)
      'packages' => 
        array (size=2)
          'package1' => 
            array (size=4)
              'package_name' => string 'package1' (length=16)
              'current_version' => string 'package1-0.35.1-r0' (length=26)
              'latest_version' => string '0.35.1-r0' (length=9)
              'package_status' => string '=' (length=1)
          'package3' => 
            array (size=4)
              'package_name' => string 'package3' (length=3)
              'current_version' => string 'package3-5.3.2-r0' (length=33)
              'latest_version' => string '5.3.2-r0' (length=8)
              'package_status' => string '=' (length=1)
  '_id' => float 5

Here's the code that I use to try to insert this array into my mongo database:

 68 function add_history_record($location)
 69 {
 70         $m = new MongoClient("mongodb://10.1.1.1:27017");
 71         $db = $m->mymongodb;
 72         $collection = $db->mycollection;
 73         $location['_id'] = getNextSequence("locationid");
 74         $cursor = $collection->insert($location);
 75 var_dump($cursor);
 76 }

The error message I'm getting is this:

Fatal error: Uncaught exception 'MongoException' with message ''.' not allowed in key: test.server.1' in /var/www/html/mongotestapp/inventory.php on line 74

What I've tried So Far:

To prove to myself that I can have keys with "." in it, I used robomongo to add the following document:

{
    "_id" : ObjectId("573483ae3747106e60a087f9"),
    "test.server.1.1" : 123
}

It saved the document in my collection no problems. I'm not sure what I'm missing in my PHP code... Any suggestions?

EDIT 1:

I've changed my code to include the following:

 68 function add_playbook_history_record($location)
 69 {
 70         $m = new MongoClient("mongodb://10.1.1.1:27017");
 71         $db = $m->phonesys;
 72         $collection = $db->inventory;
 73         $location['_id'] = getNextSequence("locationid");
 74         printf($location['_id']); 
 75         echo "<pre>".json_encode($location)."</pre>";
 76         $cursor = $collection->insert($location, array("w"=>1));
 77 
 78 //      $cursor = $collection->insert($location, array("w" => 0,"j"=>true));
 79 }

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.
I've been playing around with the different options and reading this: http://php.net/manual/en/mongo.writeconcerns.php but if you have any tips I'm all ears.

I don't have any shards or other servers. Just one main mongodb server running right now.

Thanks.


回答1:


You can not have a . in your key names as the error message tells you.

MongoDB accepts this directly, but you would not be able to query against these field names as in queries, the dot has a special meaning:

// document
{foo:{bar:"baz"}}}

// query
db.col.find( { 'foo.bar' : 'baz' } );

All official drivers guard against putting a . (and other chars) in key names, and hence the PHP driver correctly rejects this.



来源:https://stackoverflow.com/questions/37189133/mongo-php-app-error-fatal-error-uncaught-exception-mongoexception-with-messa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!