Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/orgoldfus/sql2mongo
Use SQL to query MongoDB
https://github.com/orgoldfus/sql2mongo
hacktoberfest mongo mongodb parser sql
Last synced: 19 days ago
JSON representation
Use SQL to query MongoDB
- Host: GitHub
- URL: https://github.com/orgoldfus/sql2mongo
- Owner: orgoldfus
- License: mit
- Created: 2019-10-11T10:26:11.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T04:55:21.000Z (over 1 year ago)
- Last Synced: 2024-10-10T04:05:11.902Z (about 1 month ago)
- Topics: hacktoberfest, mongo, mongodb, parser, sql
- Language: JavaScript
- Homepage:
- Size: 347 KB
- Stars: 16
- Watchers: 3
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sql2mongo [![Build Status][travis-image]][travis-url] ![npm](https://img.shields.io/npm/v/sql2mongo)
Use SQL syntax to query MongoDB
## Installation
```
npm install sql2mongo
```## Usage
### getMongoQuery
Parses an SQL WHERE clause to a mongo query object.Example:
```js
const { getMongoQuery } = require("sql2mongo");const mongoQuery = getMongoQuery(`
show = "Friends" OR
(city = "New York" AND
year BETWEEN 2005 AND 2014 AND
name IN ("Ted", "Marshall", "Barney"))
`)
```Result:
```js
{
$or: [
{ show: "Friends" },
{
city: "New York",
year: { $gt: 2005, $lt: 2014 },
name: { $in: ["Ted", "Marshall", "Barney"] },
}
]
}
```#### Nested objects
If you need to query a nested object, use the `NESTED` function inside your query.
The `NESTED` function receives a string with a WHERE clause for the nested object.Example:
If you would like to query the following nested document:
```js
{
item: 'journal',
qty: 25,
size: { h: 14, w: 21, uom: 'cm' },
status: 'A'
}
```You can use the `NESTED` function like this:
```js
const { getMongoQuery } = require("sql2mongo");const mongoQuery = getMongoQuery(`
size = NESTED("h = 14 AND w > 20")
`)
```Result:
```js
{
size: {
h: 14,
w: { $gt: 20 }
}
}
```Otherwise, you can use the dot annotation:
```js
const { getMongoQuery } = require("sql2mongo");const mongoQuery = getMongoQuery(`
size.h = 14 AND
size.w > 20
`)
```Result:
```js
{
"size.h": 14,
"size.w": { $gt: 20 }
}
```## TODO
- Add support for all DML commands (insert, update, etc.)## Build
- Run `npm run build` to build the package.## LICENSE
MIT[travis-image]: https://travis-ci.com/orgoldfus/sql2mongo.svg?branch=master
[travis-url]: https://travis-ci.com/orgoldfus/sql2mongo