Download the PHP package deerdama/laravel-mongo-helper without Composer
On this page you can find all versions of the php package deerdama/laravel-mongo-helper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-mongo-helper
Mongodb Helper for Laravel
Artisan command to quickly check, delete, export mongo collections, and to import data.
Install package: composer require deerdama/laravel-mongo-helper
.
Should work on any laravel above 5.0
, I can personally confirm 5.8
, 6.x
, 7.x
, 8.x
. Feel free to let me know if you find out issues on other versions, and I'll update the info..
- Available Parameters
- Config
- Basic Usage
- Using where Conditions
- Downloads
- Imports
- Update Data
Available Parameters
- The first and only argument (optional) of the command is the collection name. The rest are all options
Option | Value | Description |
---|---|---|
connection | string | Use a specific connection name instead of the default |
count | Output the total of matching records found in the specified collection | |
count_all | Shows a table with all existing collections and their totals | |
csv ** | Adding the option will export the data as csv (default is json) | |
delete | Delete the entire content or the matching results from the collection | |
desc | Make the sorting descending (requires --sort option) | |
download ** | Export the results into a file | |
download_path ** | string | Download the file into a specific directory (will ignore the default config directory ) |
drop | Completely drop the collection | |
dump | Simply dump() the results as they are |
|
import ** | string | Import into a collection data exported as json or csv |
limit | int | When using some data retrieval method, limit the amount of results returned |
list | Output all existing collections | |
select | array | Retrieve only specific columns |
sort | string | Field to use for sorting |
update ** | string | Field to use for sorting |
where ** | string | Where parameters |
Config
:exclamation: If you'll need to change the default config then you'll need to publish it first:
php artisan vendor:publish --provider=Deerdama\\MongoHelper\\MongoHelperServiceProvider
After publishing the package you can edit the config in config/mongo_helper.php
. All parameters can be changed
-
connection
: default name of the connection ismongodb
. You can change that in the config. Plus any specific connection name can be passed every time you are using the command, by adding theconnection
option (eg:--connection=mongo_2
)Connection = the name of your connection as it is in
database.php
The database connection driver has to be mongodb... Duh!! -
storage
: The default filesystem disk for both imports and exports is thelocal
. Uses the storage facade =>Storage::disk('local')
Local
disk = laravel's default path for local storage isstorage/app/
depending on what you have infilesystems.php
-
directory
: By default the downloads will go be in their own directorymongodb/
-
autocast_int
: (default =true
). If a numeric value is passed then it will be automatically casted as integer. Applies to both values passed for a--where
and--update
.!!! does not apply to
floats
. Eg. value5.5
WON'T be affected byautocast_int
, useautocast_float
for that. -
autocast_float
: (default =true
). If set to true then a passed numeric (float like) value will be automatically considered asfloat
."Float like" values eg:
5.5
,15.00
,-1.1520
, etc..
!!Passing a specific cast
in the parameter will always overwrite the autocasts.
Basic Usage
Simply run the artisan command db:mongo-helper
and add the correct option based on what you need, some options require a value, details about all of them can be found in the Available Parameters table.
All fatal options (delete, drop...) will ask for an extra confirmation before being executed.
Couple of simple examples
-
php artisan db:mongo-helper --count_all
- will output a simple list of all your existing collections and the amount of records in each one of them -
php artisan db:mongo-helper test_collection --dump --limit=3 --select={name,location,skill}
- will grab 3 items fromtest_collection
, will select only the specified fields.. and will simply dump the results - results can be sorted based on a specific field eg.
--sort=name
. To make the sorting descending just pass the option--desc
Using WHERE
conditions
php artisan db:mongo-helper test_collection --where="name, IN, [xyz,abc]" --where="id, BETWEEN, [5,99]" --where="deleted_at, NULL"
-
Multiple
WHERE
s can be passed to the command, however each condition needs to be passed as a separate option -
Each
WHERE
needs to be passed as a string (inside quotes), containing the column, operator and value (separated by a comma), eg.--where="some_column, <>, some_value"
. (Value not necessary forNULL
andNOT NULL
) -
All normal operators are accepted:
=
,<>
,>
,<
,IN
,NOT IN
,NULL
,NOT NULL
,BETWEEN
... -
Arrays: to pass a value as array for operators like
IN
orBETWEEN
, just wrap the value inside square brackets, eg:--where="some_column, NOT IN, [aaa,bbb,ccc]"
-
Casting :since mongo is sensitive to the content type, and by default the value parsed from the passed condition will be a
string
, you can cast thevalue
to some specific type by addingcast=??
as last parameter of the--where
condition. For example if the collection had a column namedage
and the values were stored asinteger
then passing just--where="age, >=, 18"
wouldn't return any result since the18
would be considered a string. But passing--where="age, >=, 33, cast=int"
will make sure that the value is considered as integer. Filtering by datetime can be specified in thecast
as well, eg.--where="created_at,>,2020-01-01,cast=timestamp"
Passing the value as array (inside
[]
) and also adding a specificcast=??
, will apply the specified type to each item separately, eg:--where="age, NOT IN, [15,20,100], cast=int"
(each age inside the array will be an integer)- Some data types can be detected automatically through autocasts if enabled in the configuration. Applies to array content as well.
Download Collections
-
By default the collection will be downloaded as
json
. The--csv
option is available, you need the league package to use csv formatcomposer require league/csv
!!!! eg: -
By default the downloads will be in
mongodb/
directory. You can pass a specific path for the current download, eg: - Tip.. if you need to download a collection regularly you can just add it to the
Console/Kernel.php
scheduler, eg:
Import Data
-
You can import data into a specific collection from a
json
orcsv
file. Eg:php artisan db:mongo-helper test_collection --import=test_collection_2020_01_01_03_48_51.json
- If it doesn't find the file in the full path specified then it will try to find it in the default directory
Update Data
-
Existing records can be updated through the
--update
option (multiple allowed). -
Specific records can be targeted through
--where
conditions. You'll know the amount of records affected when prompted for confirmation. -
The update option needs to contain the field name to update and the value (comma separated)
--update="field_name,new_value"
- Data Type can be specified as third optional
cast
argument, same way as with where conditions. Autocasts apply to the--update
as well
Eg. update pet
and year
for specific records in users
collection: