deerdama/laravel-mongo-helper
Composer 安装命令:
composer require deerdama/laravel-mongo-helper
包简介
Artisan command to handle mongo collections (list collections, delete, download, import data, etc..)
关键字:
README 文档
README
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
- 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
❗ 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 theconnectionoption (eg:--connection=mongo_2)Connection = the name of your connection as it is in
database.phpThe 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')Localdisk = 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--whereand--update.!!! does not apply to
floats. Eg. value5.5WON'T be affected byautocast_int, useautocast_floatfor 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
WHEREs can be passed to the command, however each condition needs to be passed as a separate option -
Each
WHEREneeds 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 forNULLandNOT NULL) -
All normal operators are accepted:
=,<>,>,<,IN,NOT IN,NULL,NOT NULL,BETWEEN... -
Arrays: to pass a value as array for operators like
INorBETWEEN, 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 thevalueto some specific type by addingcast=??as last parameter of the--wherecondition. For example if the collection had a column namedageand the values were stored asintegerthen passing just--where="age, >=, 18"wouldn't return any result since the18would 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 thecastas 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--csvoption 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.phpscheduler, eg:
$schedule->command(MongoHelper::class, [ 'collection' => 'collection_name', '--download' => true ])->dailyAt('01:00');
Import Data
-
You can import data into a specific collection from a
jsonorcsvfile. 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
--updateoption (multiple allowed). -
Specific records can be targeted through
--whereconditions. 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
castargument, same way as with where conditions. Autocasts apply to the--updateas well
Eg. update pet and year for specific records in users collection:
php artisan db:mongo-helper users --update="year,1968,cast=int" --update="pet,chicken" --where="name,like,Kiryu%"
deerdama/laravel-mongo-helper 适用场景与选型建议
deerdama/laravel-mongo-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.47k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「mongodb」 「csv」 「import」 「export」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 deerdama/laravel-mongo-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 deerdama/laravel-mongo-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 deerdama/laravel-mongo-helper 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Generates Symfony2 documents, forms and CRUD
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
A package to allow laravel/passport use with mongodb/laravel-mongodb
统计信息
- 总下载量: 1.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-04