I just trying to config the PHP access to my MongoDB database. First of all, the MongoDB driver was installed in the PHP and it was update the php.ini. The code below works without any warning or error and apparently everything is just fine:
$options = [
'username' => 'user',
'password' => '123456',
'authSource' => 'database',
];
$mongo = new MongoDB\Driver\Manager('mongodb://localhost:27017', $options);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$document = array("key" => "value", "another_key" => "another_value"); //...and so on...
$bulk->insert($document);
$result = $mongo->executeBulkWrite('db.mycollection', $bulk, $writeConcern);
But, when I check my database, nothing happened: no document were inserted!!!
What I'm doing wrong???
Related
I am using the (current? not sure, php documentation is very opaque to me) method to connect to a MongoDB from PHP:
$manager = new MongoDB\Driver\Manager("mongodb://{$user}:{$pwd}#{$url}", array("ssl" => true), array("context" => $ctx));
From there, if I want to write something I do the following:
$bson = MongoDB\BSON\fromJSON($newData);
$value = MongoDB\BSON\toPHP($bson);
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
$filter,
['$set' => $value],
['multi' => false, 'upsert' => $upsert]
);
$results = $manager->executeBulkWrite("$DB.$collection", $bulk);
var_dump($results);
All the documentation on the MongoDB PHP tutorials starts with a $collection object... and the functions thereafter seem much more user-friendly (getInsertedID... insertOne...find...findOne...etc).
For example:
<?php
$collection = (new MongoDB\Client)->test->users;
$insertManyResult = $collection->insertMany([
[
'username' => 'admin',
'email' => 'admin#example.com',
'name' => 'Admin User',
],
[
'username' => 'test',
'email' => 'test#example.com',
'name' => 'Test User',
],
]);
printf("Inserted %d document(s)\n", $insertManyResult->getInsertedCount());
var_dump($insertManyResult->getInsertedIds());
It is not clear to me, how they are actually connecting to the DB... how would I go from the $manager connection to a $collection?
On the MongoDB PHP documentation page, it says 'You can construct collections directly using the driver’s MongoDB\Driver\Manager class'. Unfortunately, a search on the resulting page doesn't include the word 'collection' other than as a side comment in a user contributed note'
Elsewhere on the MongoDB PHP reference pages, I see nowhere that the MongoDB\Manager class is described.
So, how do I get access to the many features in the MongoDB\Collection class?
I was not able to get a collection out of the Manager class, however, I was able to use the bulkWrite class to execute an insert in a secure fashion (I believe). I expect the same pattern will work for reads and updates as well.
Code snippet for those that come here after me:
//echo "Specify the cert...";
$SSL_DIR = ".";
$SSL_FILE = "XXXXXX.pem";
$ctx = stream_context_create(array(
"ssl" => array(
"cafile" => $SSL_DIR . "/" . $SSL_FILE,
))
);
//echo "Done\n";
// echo "Creating manager...";
$manager = new MongoDB\Driver\Manager("mongodb://{$user}:{$pwd}#{$url}", array("ssl" => true), array("context" => $ctx));
// echo "Done!\n";
// echo "Making BSON...";
$bson = MongoDB\BSON\fromJSON($newData);
// echo "Done!\nMaking Value...";
$value = MongoDB\BSON\toPHP($bson);
$value->_id = (string) new MongoDB\BSON\ObjectID;
// echo "Done!\nMaking Bulk...";
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert($value);
// echo "Done!\nExecuting Bulk Write";
$results = $manager->executeBulkWrite("$db.$collection", $bulk);
if($results->getInsertedCount()==1) {
echo $value->_id;
} else {
echo $results->getWriteErrors();
}
// echo "Done!\n";
New to doctrine php and I followed each instruction to the point. Downloaded doctrine using composer as instructed on http://www.doctrine-project.org/projects/dbal.html and then on my index.php I added this http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#getting-a-connection only changing the test database credentials.
include_once 'vendor/autoload.php';
$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
'dbname' => 'teastdb',
'user' => 'root',
'password' => '',
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
and this does not work (shows blank page, I even inserted both right and wrong credential for testing purpose). What is wrong?
Here is a screenshot as well
I installed MongoDB library with Composer:
composer require jenssegers/mongodb
I got that error: "Class 'MongoDB\Driver\Manager' not found",
I tried many things but problem is not solved,
But finally I noticed that the class file is really not existing in library!
I havent found it anyway...
What am I missing?
Where can I found full package for MongoDB in PHP?
Please be aware of that: similar questions are not mentioning lack of class file.
Usually your code should contain a link to 'vendor/autoload.php' with the correct path to it similar to the following:
$DB_CONNECTION_STRING="mongodb://YourCredentials";
require '../../vendor/autoload.php';
Then if you use MongoDB\Driver\Manager, a modern version of MongoDB driver, you have something such as these CRUD operations in your code:
Create a document in the collection:
$bulkWrite = new MongoDB\Driver\BulkWrite;
$doc=['name' => 'John', age => 33, profession => 'Guess what?'];
$bulk->insert($doc);
$mongoConn->executeBulkWrite('db.col', $bulkWrite);
Read document in the collection by name with a limit:
$filter = ['name' => 'John'];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$mongoConn->executeQuery('db.MyCollection', $query);
Read document in the collection by MongoDb _id with a limit:
$filter = ['_id' => new MongoDB\BSON\ObjectID( '5bdf54e6d722dc000f0aa6c2' )];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$mongoConn->executeQuery('db.MyCollection', $query);
Update document in the collection:
$bulkWrite = new MongoDB\Driver\BulkWrite;
$filter = [];
$update = ['$set' => array()];
$options = ['multi' => false, 'upsert' => false];
$bulkWrite->update($filter, $update, $options);
$mongoConn->executeBulkWrite('db.MyCollection', $bulkWrite);
Delete document in the collection - Delete:
$bulkWrite = new MongoDB\Driver\BulkWrite;
$filter = ['name' => 'John', age => 33];
$options = ['limit' => 1];
$bulkWrite->delete($filter, $options);
$mongoConn->executeBulkWrite('db.col', $bulkWrite);
image for mongo info
MongoDB shell version v3.4.1
mongodb driver v1.6.14
xampp v3.2.1, Apache/2.4.16 (Win32) OpenSSL/1.0.1p PHP/5.6.12
i m running for first time and getting this problem plz help me.
Fatal error: Class 'MongoDB\Driver\Manager' not found in H:\xampp\htdocs\www\phpmongodb\vendor\mongodb\mongodb\src\Client.php on line 81
I had the same error before I set up the mongodb added the following link to 'vendor/autoload.php'. In many cases you need to check your hosting. It started working after my code looked like the following:
$DB_CONNECTION_STRING="mongodb://YourCredentials";
require '../../vendor/autoload.php';
$manager = new MongoDB\Driver\Manager( $DB_CONNECTION_STRING );
Then if you use MongoDB\Driver\Manager, a modern version of MongoDB driver, you have something looks such as this:
Create a document in the collection:
$bulkWrite = new MongoDB\Driver\BulkWrite;
$doc = ['name' => 'John', age => 33, profession => 'Guess what?'];
$bulkWrite->insert($doc);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);
Read document in the collection by name with a limit:
$filter = ['name' => 'John'];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);
Read document in the collection by MongoDb _id with a limit:
$filter = ['_id' => new MongoDB\BSON\ObjectID( '5bdf54e6d722dc000f0aa6c2' )];
$options = ['limit' => 2];
$query = new MongoDB\Driver\Query($filter, $options);
$manager->executeQuery('db.MyCollection', $query);
Update document in the collection: (Read more about options upsert and multi here)
$bulkWrite = new MongoDB\Driver\BulkWrite;
$filter = ['name' => 'John'];
$update = ['$set' => ['name' => 'Smith', age: 35, profession => 'Guess what?']];
$options = ['multi' => false, 'upsert' => false];
$bulkWrite->update($filter, $update, $options);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);
Delete document in the collection - Delete:
$bulkWrite = new MongoDB\Driver\BulkWrite;
$filter = ['name' => 'John', age => 35];
$options = ['limit' => 1];
$bulkWrite->delete($filter, $options);
$manager->executeBulkWrite('db.MyCollection', $bulkWrite);
I have PHP-Solr extension version 1.0.3-alpha installed on my server. I'm initializing the connection using:
$options = array(
'hostname' => 'hostname',
'login' => '',
'password' => '',
'port' => '',
'path' => 'solr/core1');
$client = new SolrClient($options);
It returns the following object:
class SolrClient#17 (1) { private $_hashtable_index => int(13444) }
But whenever I try to run a query like:
$qryArray = 'key_id:123456';
$client->deleteByQuery($qryArray);
It throws an exception as follows:
Solr HTTP Error 7: 'Couldn't connect to server
Can anyone help me to find what would causing this issue?
I have tried var_dump($client->getDebug());, but it returns NULL.