1.2.1 • Published 3 years ago

sql2mongo v1.2.1

Weekly downloads
68
License
MIT
Repository
github
Last release
3 years ago

sql2mongo Build Status npm

Use SQL syntax to query MongoDB

Installation

npm install sql2mongo

Usage

getMongoQuery

Parses an SQL WHERE clause to a mongo query object.

Example:

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:

{
  $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:

{
  item: 'journal',
  qty: 25,
  size: { h: 14, w: 21, uom: 'cm' },
  status: 'A'
}

You can use the NESTED function like this:

const { getMongoQuery } = require("sql2mongo");

const mongoQuery = getMongoQuery(`
  size = NESTED("h = 14 AND w > 20")
`)

Result:

{
  size: {
    h: 14,
    w: { $gt: 20 }
  }
}

Otherwise, you can use the dot annotation:

const { getMongoQuery } = require("sql2mongo");

const mongoQuery = getMongoQuery(`
  size.h = 14 AND 
  size.w > 20
`)

Result:

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

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

5 years ago