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
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'm trying to make a thumbnail in a Laravel project and I tried a lot other ways but with no success - Libraries, APIs... The problem is that the project is developed in Windows and the professor needs it in Windows as well.
So far, I experimented to integrate different libraries (wkhtmltoimage, spatie/browsershot, mpdf and s.o.) but in the most of the cases, there are problems with the path.
The required function, which I need, works very good in command prompt and I thought that I must find a way to call it in the controller.
I've tried with:
shell_execute($call);
system($call);
exec($call);
// with $call = "{func_name} {path_to_input_file} {name_of_output_file}";
// Example: $call = "wkhtmltoimage C:\xampp\htdocs\app\public\test.html img.jpg"
But no result. The function generates an image, which I want to store in the database.
Is there an other way to make a call to the command prompt?
Maybe SSH call?
You can execute Artisan commands directly via your controller.
Look at this example from the Laravel documentation:
Route::get('/foo', function () {
$exitCode = Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
});
I'm trying to build a small application on Laravel with modular approach, I am having a controller method which seeds the database as per the module/plugin name:
I have something like this:
Artisan::call('db:seed --class=Nitseditor\\Plugins\\'.$pluginName.'\\Databases\\seeds\\InstallSeeder');
Whenever I am calling this I am getting this error in my console.
Class NitseditorPluginsConfidenceDatabasesseedsInstallSeeder does not exist
I don't know why it remove \ and concatenate the strings.
How can I achieve this?
You can do:
$fullClassName = "Nitseditor\\Plugins\\${pluginName}\\Databases\\seeds\\InstallSeeder";
Artisan::call("db:seed", ['--class' => $class]);
in my case when i have some module in subfolders
then want to run one of the Seeders directly without running other seeders
php artisan db:seed --class=WM\Common\Seeder\SmsStatusSeeder
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.
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.