I am using MongoDB 2.6 with two shard clusters config.
I want to call a function dataStats() that I create and store in MongoDB. This is my PHP script:
$client = new Mongo();
$db = $client->mydata;
$db->system->js->save(array("_id"=>"dataStats",
"value"=>new MongoCode("function() { ... }")));
$db->execute("dataStats()");
This code gives me this error:
'err' => 'Error: can\'t use sharded collection from db.eval',
'code' => 16722
The reason is $db->execute method is using Mongo db.eval command which is not supported with sharded collections. Is there a workaround for this issue? How can we call a stored procedure in sharded MongoDB from PHP?
There's no workaround. db.eval doesn't work with sharded collections. You should avoid using it if at all possible, anyway.
Related
The current PHP MongoDB client doesn't include an explain() feature in the query builder. How the explain information could be retrieved for the query below?
use MongoDB\Client;
$client = new Client($mongoUri);
$database = $client->selectDatabase('someDB');
$collection = $database->selectCollection('collectionName');
$results = $collection->find(['key' => 'value'], ['sort' => ['key2' => -1]]);
MongoDB client: https://www.php.net/manual/en/class.mongodb.php
MongoDB::command could be a way to the solution. https://www.php.net/manual/en/mongodb.command.php
Note! There is a deprecated mongodb client that has this feature.
One way of doing it:
Get the find command that the driver sends to the database for your query. It would look like this. Generally these can be obtained from command monitoring, hopefully this works.
Send an explain command giving the find command as the argument using the driver-provided facility to send arbitrary commands to the database. Or, use mongo shell for this part.
I use this MongoDb driver in my application. I like it, because it works well. I can run simple select and insert statements, like:
$em = new MongoDB\Driver\Manager("mongodb://localhost:27017/testdb");
$query = new Query(['name' => 'John']);
$res = $em->executeQuery('users', $query);
But the problem is, I can not find even a single example on making aggregations. PHP documentation does not say a word about this. While, MongoDb documentation seems to use another library:
$collection = (new MongoDB\Client)->test->restaurants;
$cursor = $collection->find([
'name' => new MongoDB\BSON\Regex('^' . preg_quote('(Library)')),
]);
It seems like an example from another library, because MongoDb driver does not have this MongoDB\Client class. There is such a class, but in a deprecated library. So, what is the right way to make aggregations in PHP, using modern MongoDb driver?
This extension provides a minimal API for core driver functionality: commands, queries, writes, connection management, and BSON serialization.
https://www.php.net/manual/en/set.mongodb.php
To execute aggregate command, use MongoDB\Driver\Command
{
aggregate: "<collection>" || 1,
pipeline: [ <stage>, <...> ],
explain: <boolean>,
allowDiskUse: <boolean>,
cursor: <document>,
maxTimeMS: <int>,
bypassDocumentValidation: <boolean>,
readConcern: <document>,
collation: <document>,
hint: <string or document>,
comment: <string>,
writeConcern: <document>
}
https://docs.mongodb.com/manual/reference/command/aggregate/#syntax
I'm using following:
PHP 7.2
MongoDB 3.4
Pecl 1.5.2
I'm working on a Laravel project. It uses MongoDB as database. I have few collections on which I have to create Mongo Views using Laravel migration. I was wondering whether its possible to create Mongodb Views using PHP. Currently I have a work around. I have created a JavaScript file which has MongoDB db.createView() query in it. It also takes view name and collection name as parameters. Following is my work around. $db has database name, $view has view name, $collection has collection name and $script has the path to the JavaScript file. This code I'm writing in migration class's up() method.
$cmd = "mongo $db --eval \"var view='$view', collection='$collection'\" $script";
exec($cmd);
In my Javascript file, I have code something like following
db.createView(view, collection, <aggregate query>);
So as everyone can see, I'm running terminal command from PHP to make views. So is there any PHP function in mongo library to make mongo views?
If you're using mongo with Laravel, I'm going to assume you're using jenssegers/mongodb to use it with Eloquent.
So, let's assume you have your mongo database set up as your 'mongodb' database connection. You need the MongoDB\Database for your database. You can get this with:
$mongo = app('db')->connection('mongodb')->getMongoDB();
Of course, if you're not using jenssegers/mongodb, you can still do the same thing with mongodb/mongodb as well.
$mongo = (new MongoDB\Client)->selectDatabase($db);
This has a method called command (see https://docs.mongodb.com/php-library/current/reference/method/MongoDBDatabase-command/), which corresponds to the db.runCommand method from the mongo cli. db.createView calls that method (see https://docs.mongodb.com/manual/reference/method/db.createView/#db.createView)
So, you can use $mongo->command to create the view like this:
$mongo->command([
'create' => $view,
'viewOn' => $collection,
'pipeline' => $aggregateQuery,
'collation' => ['locale' => 'en'],
]);
You can use this library mongoPhpLibrary
This will make your work easy
I'm using DB::connection('mongodb')->getPdo() to check if the database is connected (if not phpunit will mark some test as skipped) and it's turning out that ->getPdo() always return null. The connection is working proven by that I can use php artisan migrate to create new collection and I can insert data into the collection.
Environment :
OS : windows 10 running MAMP 3.2.0
laravel : 5.2.37
jenssegers/mongodb : 3.0.2
Edit 1: Add returned object from using DB::connection('mongodb')->getMongoDB()->connected
MongoDB\Collection {#647
+collectionName: "connected",
+databaseName: "destinycore",
+manager: MongoDB\Driver\Manager {#640},
+readConcern: MongoDB\Driver\ReadConcern {#642},
+readPreference: MongoDB\Driver\ReadPreference {#643},
+typeMap: [
"array" => "MongoDB\Model\BSONArray",
"document" => "MongoDB\Model\BSONDocument",
"root" => "MongoDB\Model\BSONDocument",
],
+writeConcern: MongoDB\Driver\WriteConcern {#644},
}
While confusing, it's actually correct. This connection class extends from the base class provided by Laravel. It doesn't return a PDO instance because this is a MongoDB connection. It doesn't actually use PDO, which is only used for SQL-related databases. If you're looking for the "raw" connection for this package, you should use the getMongoDB method, which returns a MongoDB\Client instance.
Using the example from the manual:
$mongo = new Mongo("mongodb://sf2.example.com,ny1.example.com", array("replicaSet" => "myReplSet"));
When I check $mongo, it says it is indeed connected. I thought I could then call $mongo->isMaster() to get replica set details, but that doesn't work. Is that not a proper way of doing this?
isMaster isn't a PHP function (see http://www.php.net/manual/en/class.mongo.php for a list of functions available in the Mongo class). You can do:
$result = $mongo->myDb->command(array("isMaster" => 1));
This runs the isMaster command on the myDb database (it doesn't matter what db you run it on).