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

Community
  • 1
  • 1
Musher
  • 85
  • 1
  • 2
  • 7
  • are you selecting a db and collection prior to inserting? – Petrogad Nov 09 '12 at 15:03
  • yes, $m->setDatabase("test"); $m->setCollection("test"); $m->insert(array( 'url' => 'http://www.query7.com', 'software' => 'wordpress', 'tutorials' => array('php','javascript','web development'), )); – Musher Nov 09 '12 at 15:05
  • You're attempting to save a document that is larger than MongoDB can save. – GBD Nov 09 '12 at 15:05
  • @GDB, i try this: insert(array(test=>'test')) then i get same error. 145 bytes is large? – Musher Nov 09 '12 at 15:08
  • Has any changs been done your server? It looks as though for some reason the global max size for bson documents has been erased either in PHP driver or mongodb, so have you had any changes to your server recently? – Sammaye Nov 09 '12 at 15:22
  • Try this `db.isMaster().maxBsonObjectSize/(1024*1024)+' MB'` in you `mongo` command shell, taken from [here](http://stackoverflow.com/questions/4667597/understanding-mongodb-bson-document-size-limit#comment9690769_4667597). What does it return? – juan.facorro Nov 09 '12 at 15:22
  • @juan.facorro > db.isMaster().maxBsonObjectSize/(1024*1024)+' MB' NaN MB > – Musher Nov 09 '12 at 15:30
  • @MustafaHergül What about `db.isMaster()` alone? – juan.facorro Nov 09 '12 at 15:32
  • @juan.facorro { "ismaster" : true, "ok" : 1 } – Musher Nov 09 '12 at 15:35
  • There are a few other SO's with this and all where due to the mongo server being at a very old version.. It would be best to at least upgrade prior to any further debugging.. – Petrogad Nov 09 '12 at 15:38
  • @Sammaye i have just setup mongodb server on mycomputer. i dont change anything – Musher Nov 09 '12 at 15:40
  • I have used mongodb since 0.8 I have never heard of what Frederico speaks of however I would say you might have a faulty MongoDB as such I would reinstall and you might as well reinstall with the latest version. It is possible you got a bad download, v1.6 would have been from a very very very old repo, clearly out of date. What OS is this on? – Sammaye Nov 09 '12 at 15:41
  • @Frederico i used http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ for install server. how can i update mongodb – Musher Nov 09 '12 at 15:49
  • @SalvadorDali versinon: 1.6.3 but i remove that and i installing mongodb20-10gen now – Musher Nov 09 '12 at 16:21
  • I update mongodb to 2.0.7 and it works . thanks ... – Musher Nov 09 '12 at 16:38

4 Answers4

1

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

Petrogad
  • 4,405
  • 5
  • 38
  • 77
  • i have tried this and same error:Fatal error: Uncaught exception 'MongoException' with message 'size of BSON doc is 145 bytes, max is 0' in /home/hergul/www/democratusmessage/index.php:13 Stack trace: #0 /home/hergul/www/democratusmessage/index.php(13): MongoCollection->insert(Array) #1 {main} thrown in /home/hergul/www/democratusmessage/index.php on line 13 – Musher Nov 09 '12 at 15:12
  • @MustafaHergül what version of mongo are you currently using? – Petrogad Nov 09 '12 at 15:15
  • yikes.. You need to really update that. It's likely your php driver is built for a later version than what you're at. – Petrogad Nov 09 '12 at 15:22
  • Web server Apache/2.2.17 PHP version PHP 5.3.5-1ubuntu7.11 PHP extension mongo/1.3.0RC2-dev – Musher Nov 09 '12 at 15:23
  • Where did you install MongoDB from, the repos? I didn't know 7.11 repos were still mantained – Sammaye Nov 09 '12 at 15:42
  • @Sammaye i used this document : http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/ for install – Musher Nov 09 '12 at 15:48
1

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.

Sammaye
  • 43,242
  • 7
  • 104
  • 146
  • Yes - I've just had same error on a brand new Debian 6 (Squeeze) install with php5-cli (v5.3.3-7) and the default Debian mongoDB (which is **only 1.4.4**). The good news is you can just add an external repo (10gen) rather than compiling from source: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian/ - that gave me Mongo **v2.2.2**. You should also, if you're setting up a new machine, install a 64-bit version of the operating system as Mongo databases can only be 2GB under 32 bit). That's why I was reinstalling it today... – William Turrell Dec 08 '12 at 13:39
  • The default download from MongoDB site: http://www.mongodb.org/downloads comes as an executable binary. It isn't deb packaged but it is the contents of the deb package put into a folder, all you need to do is write a service script and it will be the same as installing from the repos. – Sammaye Dec 08 '12 at 13:49
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.

juan.facorro
  • 9,791
  • 2
  • 33
  • 41
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

Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753