5
Route::get('/', function () {
    $tweets = Tweet::all();
    return view('welcome', ['tweets' => $tweets]);
});

I am making a laravel app using mongodb.

When I go to '/', I get an error in the mongod terminal that says

AssertionException handling request, closing client connection: 10304 Client Error: Remaining data too small for BSON object

This is my tweet model (in App\Tweet):

namespace App;

use Jenssegers\Mongodb\Model as Eloquent;

class Tweet extends Eloquent {

    protected $collection = 'tweets_collection';

}

2 Answers2

2

There are at least two reasons why this issue (Client Error: Remaining data too small for BSON object) appears:

1. PHP MongoDB driver is not compatible with MongoDB installed on the machine.
(originally mentioned in the first answer).

Examine PHP driver version set up on your machine on <?php phpinfo(); page:

enter image description here

Retrieve MongoDB version in use with:

mongod --version\
# db version v3.2.0

Use compatibility table on MongoDB website to see whether examined PHP MongoDB driver version is compatible with MongoDB version:

enter image description here

If versions are not compatible, it is required to uninstall one of the existing parts and install compatible version. From my own experience, it is much easier to change PHP MongoDB driver, since only different .so extension file is required.

2. Two PHP MongoDB drivers are installed on the machine.

Since MongoClient is deprecated, many tutorials and articles online (including official mongo-php-driver repository on Github) now guides to install mongodb, not mongo PHP driver. Year+ before, everyone was pointing at mongo extension, however.

Because of this change from mongo to mongodb, we might get both extensions defined in php.ini file. Just make sure, only one extension is defined under "Dynamic Extension" section:

enter image description here


Hope somebody gets this answer useful when looking for a solution to fix "Remaining data too small for BSON object" error working with MongoDB through PHP MongoDB driver.

Community
  • 1
  • 1
Nik Sumeiko
  • 8,263
  • 8
  • 50
  • 53
0

The issue was that Laravel was unable to communicate with MongoDB because I was using the mongodb-1.1 php driver and MongoDB 3.2 together. According to the table found on this page: https://docs.mongodb.org/ecosystem/drivers/php/, these two versions are not compatible. I uninstalled MongoDB 3.2 and installed MongoDB 3.O, and the problem was solved.