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
Related
I am currently using the MongoDB driver for PHP:
http://php.net/manual/en/set.mongodb.php
However I would also like to be able to use the legacy driver (Mongo for PHP) for compatibility purposes:
http://php.net/manual/en/book.mongo.php
Is there any way that I could use both at the same time in the same PHP script?
The only possibility is to use a wrapper, you can find some thing here PHP MongoDB Frameworks
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.
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.
I've noticed MySQL could use Memcached NoSQL with InnoDB but I can't retrieve information about how to use it.
I want to use with PHP.
Are NoSQL queries standard?
First of all, MySQL only support memcached with NoSQL since version 5.6. Today this version it's not update yet in linux repositories and must be manually installed, specially in servers, e.g. MySQL --version (ubuntu) is 5.5.38 ; (RedHat server) 5.1
You also must install libevent-dev e.g:
Still some hacks are needed, and you must install memcache interface plugin for MySQL located in $MYSQL_HOME/share.
I found a well explained post how to install and try it:
http://chipersoft.com/p/MySQL-via-Memcache/
Due to its young state, it's not for production servers.
Use it with PHP is easy because memcache is a native module in PHP when you have php5-cli and php5-memcache installed:
<?php
$memcache = new Memcache;
if (!$memcache->connect('localhost', 11211)) throw new Exception("Could not connect");
if (!$memcache->set('bar', 'John|Smith')) throw new Exception("Could not store value");
$memcache->get('##aaa'); //switch containers
$result = $memcache->get('AA');
var_dump($result);
Conclusion, this method let you work with MySQL and NoSQL simultaneously. Memcache offers a quick, NoSQL way to retrieve and work with data in a well different manner than SQL.
Edit: Another useful link was this post by Tony Darnell
After successfully upgrading from Mongo 2.4.10 to Mongo 2.6.0 (I verified that I can query the database in the shell) I got some problems with my previously working php code.
Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017: Remote server has closed the connection' and also points me to the line which looks like: $m = new Mongo();
I am using Php 5.5.10-1+deb.sury.org~precise+1 with 1.4.5 phpMongo Driver (which I think has not been upgraded to support 2.6 version). Anyone experienced the same issue after 2.6 upgrade?
You always want to upgrade the driver before upgrading the server.
The MongoDB PHP Driver version 1.5.0 (and later) have MongoDB 2.6.0 (and later) support, where as certain things will not work when using the 1.4.x (or older) releases of the MongoDB PHP Driver.
I strongly recommend you upgrade to the 1.5.1 release of the MongoDB PHP Driver.
Furthermore, the "Mongo" object has been deprecated, you really want to use the "MongoClient" class.
That being said. This should work just fine.
What does your mongod.log say?
Can you connect to localhost:27017 using the mongo shell?
Actually everything appeared to be simpler than that (I should blame only me and not php mongo driver):
I was using new Mongo(); to connect to database (which is apparently deprecated)
Instanciating this class will emit E_DEPRECATED warning, and turn off
acknowledged writes.
Strangely enough I never saw this in my logs although I have E_ALL as my warning level. Anyway after I changed to recommended way new MongoClient(); the problem was solved.