mongo PHP应用程序错误:致命错误:消息为“"的未捕获异常'MongoException'.不允许输入密钥 [英] mongo PHP app error: Fatal error: Uncaught exception 'MongoException' with message ''.' not allowed in key

查看:78
本文介绍了mongo PHP应用程序错误:致命错误:消息为“"的未捕获异常'MongoException'.不允许输入密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下PHP数组:

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

这是我用来尝试将此数组插入到mongo数据库中的代码:

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 }

我收到的错误消息是:

致命错误:消息为"的未捕获异常'MongoException'.不是 允许在密钥:test.server.1'中输入 /var/www/html/mongotestapp/inventory.php上 第74行

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

到目前为止,我已经尝试过:

向我证明我可以使用."作为键.在其中,我使用了robomongo添加了以下文档:

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
}

它将文档保存在我的收藏中没有问题. 我不确定我的PHP代码中缺少什么... 有什么建议?

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

我已更改代码以包括以下内容:

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 }

我可以看到系统正在生成合法的位置ID. 我还采用了json格式的输出,并在jsonlint中进行了测试,以确保其格式正确等. 最后,我开始使用写选项.当我启用第78行时,系统不会返回任何错误消息,但是记录不会添加到数据库中.但是,当我设置w = 1时,它将失败,并显示关于密钥的相同错误消息.
我一直在研究不同的选项,并阅读以下内容: http://php.net /manual/en/mongo.writeconcerns.php 但是,如果您有任何建议,我都会不知所措.

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.

我没有任何分片或其他服务器.现在只有一台主要的mongodb服务器正在运行.

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

谢谢.

推荐答案

错误消息告诉您,您的键名中不能包含..

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

MongoDB直接接受此操作,但是您将无法像查询中一样查询这些字段名称,点具有特殊含义:

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' } );

所有官方驱动程序都禁止在键名中放置.(和其他字符),因此PHP驱动程序正确拒绝了此操作.

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

这篇关于mongo PHP应用程序错误:致命错误:消息为“"的未捕获异常'MongoException'.不允许输入密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆