# mongo-to-sql

Latest version **1.3.2** (published 2018-05-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongo-to-sql
pnpm add mongo-to-sql
yarn add mongo-to-sql
bun add mongo-to-sql
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.2 |
| Published | 2018-05-28 |
| First published | 2018-03-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 104.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Fabian Enos |
| Maintainers | fabiantmc |

## Links

- npm: https://www.npmjs.com/package/mongo-to-sql
- Repository: https://github.com/fabianTMC/mongoToSQL
- Homepage: https://github.com/fabianTMC/mongoToSQL#readme
- Issues: https://github.com/fabianTMC/mongoToSQL/issues
- npm.io page: https://npm.io/package/mongo-to-sql

## Dependencies (2)

- [mysql](https://npm.io/package/mysql.md) ^2.13.0
- [shortid](https://npm.io/package/shortid.md) ^2.2.8

## Recent versions

- 1.3.2 (latest) — 2018-05-28
- 1.3.1 — 2018-05-27
- 1.3.0 — 2018-05-27
- 1.2.2 — 2018-05-25
- 1.2.1 — 2018-03-17
- 1.2.0 — 2018-03-14

## README

# MongoToSQL - Convert MongoDB aggregation pipelines to their SQL equivalent


# Notes
* Some pipelines throw errors so it would be safer to wrap all library function call in a try..catch block.
* When a `$match` stage is immediately followed by a `$project` stage, an optimization will kick in where the headless output of the `$project` will be appended with the headless output of the `$match` to avoid an unnecessary subquery. 

## Supported pipelines
----
* $group
* [$project](docs/project.md)
* $match
* $lookup

## Supported $group operators
----
* $sum
    * NOTE: $sum currently does not support nested operators or multiple expressions through an array.

## $lookup
----
$lookup as per the [MongoDB documentation](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/) performs a left outer join. This behaviour has been mirrored here. To change the type of join, please specify the `joinType` key in the $lookup object.

The difference with the `as` key is that it takes an object that will map from the result to the table that is being joined with.

For example:
```javascript
    $lookup({
        from: "states",
        localField: "state_id",
        foreignField: "id",
        as: {
            stateName: "name",
            stateId: "id"
        }
    })
```
will return

```sql
    SELECT t2.name as stateName, t2.id as stateId FROM (SELECT * FROM currentTable) t1 LEFT JOIN (SELECT * FROM states) t2 ON t1.state_id = t2.id
```

## $match usage
```javascript
$match(matchObject, tableName, options)
```

* options (optional): A hashmap of options 
    * headless: Should the `SELECT * FROM tableName` be included. Defaults to `true`.

Example usage
```javascript
$match({
    status: "D",
    qty: 2
}, 
"inventory", 
{
    headless: true
});
```
will return

```sql
WHERE status = 'D' AND qty = 2
```

Without the headless option specified, it will return
```sql
SELECT * FROM inventory WHERE status = 'D' AND qty = 2
```


## $match notes
* This library currently only supports comparisons with numbers. Strings and arrays are not compared and will cause an error when used with $eq despite (MongoDB's support for the same)[https://docs.mongodb.com/manual/reference/operator/query/eq/#match-an-array-value].

* $ne will only match values and not strings at the moment.

* Only arrays can be passed as the value to the $in operator.


# Example Usage
-------
For a complete understanding and set of examples for how to use this library, please refer to the **tests** folder.

Using $sum:
```
let collectionName = "loginstore";

mongoToSQL.convert(collectionName, [
    {"$group": {
        count: {
            "$sum": 1
        },
        age: "$age"
    }}
]);
```

---
_Source: https://npm.io/package/mongo-to-sql · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
