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';
}
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:
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:
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:
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.
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.
Related
I am upgrading my Ubuntu server and installing latest version of PHP i.e PHP 7.0.18, MongoDB 3.2.14, CodeIgniter v 2.2. Now when I am trying to connect with Mongo through CodeIgniter I am getting error as:
The MongoDB PECL extension has not been installed or enabled.
Kindly help. Kindly note my MongoDB and app are in two different instances. From app instance through CLI I can access the MongoDB but through CodeIgniter I am not able to.
Actually I am not able to update the lib file which I am using as per the updated versions.
Thanks.
Go to php.ini and search for the extension and pass the proper extension path like:
extension="/usr/lib/php/extensions/no-debug-non-zts-20100525/mongo.so"
uncomment it by removing the ; from the starting and restart the apache and try again.
Using Laradock (basically a set og Docker images for Laravel development), I keep getting Class 'MongoId' not found FatalThrowableError errors when calling new \MongoId( $id ) in PHP.
This post Class 'MongoId' not found (Zend Framework with MongoDB Doctrine) suggests that the reason for given error is that the PHP Mongo extension isn't enabled.
However, if I look at the phpinfo() output, I can see mongodb section. Doesn't that mean it's enabled?
What else could possibly cause this error?
I assume that you are using php 7 version.
In php 7 version a new MongoDB extension is used.
So instead of legacy MongoId you should use MongoDB\BSON\ObjectID
I'm trying to connect PHP 7 with mongoDB, I installed the "new" MongoDB driver using pecl by following this page instructions. I can see MongoDB version 1.1.8 from phpInfo() output, but I can't figure out how to initiate a connection from PHP code :p . the following code includes my attempts to connect (tried to connect even using old fashion way)
// new fashion way
$connection = new MongoDB\Driver\Client();
// or by using old fashion way
$conn = new MongoClient();
// random try :p
$randConn = new MongoDB\Client();
and in both cases, I'm getting not defined class exception.
please let me know what I'm missing and where is my mistake, please provide and example to be easier to follow if possible ;) .
PS: used operating system is ubuntu 14.04 LTS.
thanks in advance.
The page that you are referring to is the low-level PHP driver for MongoDB. The API is the same as the HHVM driver for MongoDB. The documentation for both of them is the same, and can be found at http://docs.php.net/manual/en/set.mongodb.php
The driver is written to be a bare bone layer to talk to MongoDB, and therefore misses many convenience features. Instead, these convenience methods have been split out into a layer written in PHP, the MongoDB Library. Using this library should be your preferred way of interacting with MongoDB.
The library needs to be installed with Composer, a package manager for PHP. See also Get Composer: Installation on Linux/OSX
For example:
composer require "mongodb/mongodb=^1.0.0"
Once you have it installed, you can try connecting using:
<?php
require 'vendor/autoload.php';
$collection = (new MongoDB\Client("mongodb://127.0.0.1:27017"))->dbname->coll;
?>
See also:
Doc: MongoDB PHP Library
MongoDB PHP Library: Getting Started
PHP MongoDB Driver
I have MongoDB installed in my cPanel/WHM CentOS server.
I have the PHP Drivers installed.
I have Port 27017 opened.
This works $m = new MongoDB\Driver\Manager();
These do not work:
$m = new Mongo();
$m = new MongoClient();
Both result in Fatal error: Class 'Mongo' not found in ... and Fatal error: Class 'MongoClient' not found in ....
Does anyone know why?
Not a duplicate. That SO Q is for a Windows machine. My server is Linux/CentOS
From My Hosting Provider's Sys Admin:
It looks like "mongo" is a seperate php module from "mongodb", and that "mongodb" is the newer one:
root#host [~/support/642192]# pecl search mongo
Retrieving data...0%
.Matched packages, channel pecl.php.net:
Package Stable/(Latest) Local
mongo 1.6.12 (stable) MongoDB database driver (legacy)
mongodb 1.1.2 (stable) 1.1.2 MongoDB driver for PHP
Did you need the legacy module "mongo" instead of "mongodb"? Here is hte pecl page for the package you have:
https://pecl.php.net/package/mongodb
and here is the one for the legacy module:
https://pecl.php.net/package/mongo
Short answer
You cannot use the following classes with the new mongodb driver:
$m = new Mongo();
$m = new MongoClient();
This corresponds to the legacy mongo driver. Instead you should use MongoDB\Client through the MongoDB PHP Library.
Long answer
All right. I lost some hair on this story as well, because the documentation about Mongo and PHP is extremely confusing. The options are then to get mad, bald, or both. But i found courage and finally got it. So this might help you.
1. MongoDB driver: mongo vs mongodb
First of all, you must clarify which MongoDB driver you use: either mongo (legacy) or mongodb (new). Note the smaller case. First source of confusion, the mongo driver is sometimes referred to as MongoDB (legacy) PHP driver.
https://docs.mongodb.com/ecosystem/drivers/php/
Second source confusion, the version numbers are not logical, as mongo driver had 1.5, 1.6 but mongodb starts again from 1.0. So, going forward, but backwards, it's just insane... Imo they should have started from 2.0!
The choice of the driver depends on your PHP version:
PHP5.3: you can only use the mongo legacy driver (1.5, 1.6)
PHP7.0: you can only use the new mongodb driver (1.1+)
PHP5.4, 5.5, 5.6: here you have the choice between the old mongo (1.5, 1.6) or the new mongodb (1.0, 1.1+)
But it's not over. The driver is just a low-level interface (aka PHP extension). Now we come to the programmer's API and it becomes even worse.
2. API: MongoClient vs MongoDB\Driver
Third source of confusion, the low-level drivers and the API classes have overlapping names.
mongo legacy driver -> classes MongoClient, MongoDB (!), ...
mongodb driver -> classes MongoDB\Driver, MongoDB\BSON, ...
But it's not over yet. The old mongo driver could be used directly. The new mongodb driver provides classes (such as MongoDB\Driver) but it is actually a low-level API. You are not supposed to use it directly, you could, but it's not convenient. Instead you should use the MongoDB PHP Library which gives an API similar to the old MongoClient classes...!
3. MongoDB PHP Library (with mongodb) -> MongoDB\Client
https://docs.mongodb.com/php-library/master/
So if you installed mongodb, you should install this MongoDB PHP Library in order to use MongoDB\Client. This class is supposed to be similar to the old MongoClient, but there are some differences such as the sort and projection.
To install this library you are advised to use the tool called Composer which allows to you download these classes into your repo. Then use the autoloader provided with Composer.
And here we come to the 4th source of confusion, MongoDB PHP Library is versioned from 1.0 even though you are using the last mongodb driver in version 1.1 ! It's certainly obvious for those who developed this stuff but very hard to follow for lambda users. Crazy confusing stuff.
There are also many other libraries above the drivers, but i don't know them at all so i won't go more into them (https://docs.mongodb.com/ecosystem/drivers/php-libraries/).
TL;DR
Depending on your PHP version, clarify which MongoDB driver to use: mongo (legacy) or mongodb (new) ?
with mongo you can use MongoClient class directly.
with mongodb you should also install MongoDB PHP Library to use MongoDB\Client class.
Good luck! :)
Looks like you haven't enabled or installed php_mongo extension.
After enabling it restart apache and check phpinfo() to see if its properly enabled.
You have use the correct namespace. As stated in their docs the namespace for the client is MongoDB\Client. Alterantivly you can use a use statement like this use MonogoDB\Client.
In PHP how to check if the CORRECT mongodb PHP driver is installed for the corresponding mongodb.
The link below tells how to check if mongodb PHP driver is installed, but does not tell if that driver is correct/compatible for the corresponding mongodb.
http://stackoverflow.com/questions/11134959/check-if-mongodb-php-driver-is-installed
For example, if I have PHP version 5.3.10 and have mongodb 2.2.2, the command
echo extension_loaded("mongo") ? "loaded\n" : "not loaded\n";
will say loaded, however, mongodb is not going to work properly because for mongodb 2.2.2, you need the latest PHP not 5.3.10
I think, you're talking about driver, not PHP itself. PHP has no built-in support to access \Mongo* classes until you compile and load special extension.
Since you're talking about the latest version of MongoDB, I think you couldn't use some parts of its functionality because you had some old driver (say, 1.2.12). When you upgraded PHP, you, probably, updated the driver to the latest (1.3.0) stable version as well. That version of driver was submitted a couple of days ago, and it supports all the latest features MongoDB provides.
Anyway, if you'd like to check which version of driver you have, you can call phpinfo(8) from your PHP and look though the output for the mongo section, where the version of driver is displayed.