sql2mongo

1.2.1 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i sql2mongo

Weekly Downloads

102

Version

1.2.1

License

MIT

Unpacked Size

46.8 kB

Total Files

18

Last publish

Collaborators

  • orgoldfus