Musher
Musher

Reputation: 85

MongoDb insert error (php) - BSON doc is X bytes, max is 0

I just started using MongoDB. i can insert new records in terminal with mongo command like:

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

And i can get these records with php, like:

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

            [deneme] => 1
        )

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

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

)

But i can not insert record with php like:

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

I GET THIS ERROR: Fatal error: Uncaught exception 'MongoException' with message 'size of BSON doc is 145 bytes, max is 0' in

I have tested with different php classes but i always get same error . What is "size of BSON doc" ?

Thanks

Upvotes: 0

Views: 985

Answers (4)

Salvador Dali
Salvador Dali

Reputation: 222601

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

Upvotes: 0

juan.facorro
juan.facorro

Reputation: 9930

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.

Upvotes: 0

Sammaye
Sammaye

Reputation: 43884

This can normally occur from either:

  • 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.

Upvotes: 1

Petrogad
Petrogad

Reputation: 4423

I don't think you can use everything with $m variable; try doing something like the php docs recommends:

$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'),
));

PHP Docs about Mongo

Make sure you're at the latest mongoDB version which can be found here

Upvotes: 1

Related Questions