0

我刚开始使用 MongoDB。我可以使用 mongo 命令在终端中插入新记录,例如:

db.test.insert({var: 'data' }) .

我可以用 php 获取这些记录,例如:

Array
(
    [0] => Array
        (
            [_id] => MongoId Object
                (
                    [$id] => 509d0a4ae6c4d3ca0ba30572
                )

            [deneme] => 1
        )

    [1] => Array
        (
            [_id] => MongoId Object
                (
                    [$id] => 509d09e2a3047c588723f9bf
                )

            [deneme] => 
            [name] => aa
        )

)

但我不能用 php 插入记录,例如:

$m->insert(array(
'url' => 'http://www.query7.com',
'software' => 'wordpress',
'tutorials' => array('php','javascript','web development'),
));

我得到这个错误: 致命错误:未捕获的异常“MongoException”,消息“BSON doc 的大小为 145 字节,最大值为 0”

我已经用不同的 php 类进行了测试,但我总是得到同样的错误。什么是“BSON doc 的大小”?

谢谢

4

4 回答 4

1

我认为您不能将所有内容都与 $m 变量一起使用;尝试做类似 php 文档推荐的事情:

$m = new Mongo(); 
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');

$collection->insert(array(
    'url' => 'http://www.query7.com',
    'software' => 'wordpress',
    'tutorials' => array('php','javascript','web development'),
));

关于 Mongo 的 PHP 文档

确保您使用的是最新的 mongoDB 版本,可在此处找到

于 2012-11-09T15:09:29.673 回答
1

这通常可以通过以下任一方式发生:

  • Bad download of your MongoDB, in which case I would download the latest version from the MongoDB site: http://www.mongodb.org/downloads
  • Or mixing the very latest PHP driver with such an old version.

There has been a breaking change in the PHP driver since v1.6 of MongoDB but it was only concerned with the way PHP connected to MongoDB and a few other things. It should not have effected whether or not you can query an old version of MongoDB.

So there is a very high chance this is not the problem however, I would not rule it out.

Either way I would:

  • Upgrade from Ubuntu 7.11, it is no longer truely mantained, to 12.04
  • Upgrade to the latest MongoDB
  • Upgrade your PHP version as well

It could just be a bad mixture of all of these.

于 2012-11-09T15:50:08.313 回答
0

I've done some research and it seems that the MongoDB version you are using does not provide the maxBsonObjectSize when the db.isMaster() function is called (see here).

This might be conflicting with the PHP drivers you are using to access the MongoDB database, since they try to get the max size from the connection object (see line 604 here):

if (FAILURE == php_mongo_write_insert(&buf, Z_STRVAL_P(c->ns), a, connection->max_bson_size TSRMLS_CC)) {

You should update your MongoDB database or try to find a compatible PHP driver with the database version you are using.

于 2012-11-09T15:55:47.290 回答
0

Most probably the problem is with an old version of Mongo. Judging by your answer MongoDb insert error (php) - BSON doc is X bytes, max is 0 and the answer of https://stackoverflow.com/a/12009052/1090562

Please reply here if the problem will not disappear after a new mongo installation

于 2012-11-09T16:27:26.583 回答