PHPackages                             deerdama/laravel-mongo-helper - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. deerdama/laravel-mongo-helper

ActiveLibrary[Database &amp; ORM](/categories/database)

deerdama/laravel-mongo-helper
=============================

Artisan command to handle mongo collections (list collections, delete, download, import data, etc..)

v1.5.1(4y ago)41.5k1MITPHP

Since Jan 4Pushed 4y ago2 watchersCompare

[ Source](https://github.com/Deerdama/mongo-helper)[ Packagist](https://packagist.org/packages/deerdama/laravel-mongo-helper)[ RSS](/packages/deerdama-laravel-mongo-helper/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (2)Versions (15)Used By (0)

Mongodb Helper for Laravel
==========================

[](#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](#Available-Parameters)
- [Config](#Config)
- [Basic Usage](#Basic-Usage)
- [Using *where* Conditions](#Using-WHERE-conditions)
- [Downloads](#Download-Collections)
- [Imports](#Import-Data)
- [Update Data](#Update-Data)

Available Parameters
--------------------

[](#available-parameters)

- The first and only argument (optional) of the command is the collection name. The rest are all options

OptionValueDescriptionconnectionstringUse a specific connection name instead of the defaultcountOutput the total of matching records found in the specified collectioncount\_allShows a table with all existing collections and their totalscsv [\*\*](#Download-Collections)Adding the option will export the data as csv (default is json)deleteDelete the entire content or the matching results from the collectiondescMake the sorting descending (requires --sort option)download [\*\*](#Download-Collections)Export the results into a filedownload\_path [\*\*](#Download-Collections)stringDownload the file into a specific directory (will ignore the default config `directory`)dropCompletely drop the collectiondumpSimply `dump()` the results as they areimport [\*\*](#Import-Data)stringImport into a collection data exported as json or csvlimitintWhen using some data retrieval method, limit the amount of results returnedlistOutput all existing collectionsselectarrayRetrieve only specific columnssortstringField to use for sortingupdate [\*\*](#Update-Data)stringField to use for sortingwhere [\*\*](#Using-WHERE-conditions)stringWhere parameters---

Config
------

[](#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 is `mongodb`. You can change that in the config. Plus any specific connection name can be passed every time you are using the command, by adding the `connection` 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 the `local`. Uses the storage facade =&gt; `Storage::disk('local')`

    *`Local` disk = laravel's default path for local storage is `storage/app/` depending on what you have in `filesystems.php`*
- **`directory`**: By default the downloads will go be in their own directory `mongodb/`
- **`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. value `5.5` WON'T be affected by `autocast_int`, use `autocast_float` for that.
- **`autocast_float`**: (default = `true`). If set to true then a passed numeric (float like) value will be automatically considered as `float`.

    "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
-----------

[](#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](#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  [![](https://camo.githubusercontent.com/397131841aebda5882fc4babc35fe866b28eda5332c1a76c8126f9dfc7ce82a5/68747470733a2f2f696d61676573322e696d67626f782e636f6d2f65622f65312f677073686c5052565f6f2e706e67)](https://camo.githubusercontent.com/397131841aebda5882fc4babc35fe866b28eda5332c1a76c8126f9dfc7ce82a5/68747470733a2f2f696d61676573322e696d67626f782e636f6d2f65622f65312f677073686c5052565f6f2e706e67)

- **`php artisan db:mongo-helper test_collection --dump --limit=3 --select={name,location,skill}`** - will grab 3 items from `test_collection`, will select only the specified fields.. and will simply dump the results

     [![](https://camo.githubusercontent.com/8035f090d5f86df2459d4715c2fddabf525fc729dbb772ee6fe8d78516265b7b/68747470733a2f2f696d61676573322e696d67626f782e636f6d2f30652f34342f65316d564a4b78325f6f2e706e67)](https://camo.githubusercontent.com/8035f090d5f86df2459d4715c2fddabf525fc729dbb772ee6fe8d78516265b7b/68747470733a2f2f696d61676573322e696d67626f782e636f6d2f30652f34342f65316d564a4b78325f6f2e706e67)
- 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
------------------------

[](#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 for `NULL` and `NOT NULL`)
- All normal operators are accepted: `=`, ``, `>`, `
