q2m

1.4.1 • Public • Published

q2m

A URL query object to MongoDB query

Why

I needed to query MongoDB database from the URL. Basically transfrom the params passed via GET to a properly written MongoDB query object.

Example

First, get the library via npm install q2m or yarn install q2m. Then:

const { parse } = require('q2m');

parse({ foo: 'bar' });
// { foo: { $eq: 'bar' }}

Mapping

(The conversion from query string to object in the examples below is made with qs )

$eq

Query string: foo=bar

Object:

{
  "foo": "bar"
}

Output:

{
  "foo": {
    "$eq": "bar"
  }
}

$ne

Query string: foo=!bar

Object:

{
  "foo": "!bar"
}

Output:

{
  "foo": {
    "$ne": "bar"
  }
}

$exists

Query string: foo==bar

Object:

{
  "foo": "=bar"
}

Output:

{
  "foo": {
    "$exists": true
  }
}

$exists

Query string: foo=!=bar

Object:

{
  "foo": "!=bar"
}

Output:

{
  "foo": {
    "$exists": false
  }
}

$gt

Query string: foo=>20

Object:

{
  "foo": ">20"
}

Output:

{
  "foo": {
    "$gt": 20
  }
}

$gte

Query string: foo=>=20

Object:

{
  "foo": ">=20"
}

Output:

{
  "foo": {
    "$gte": 20
  }
}

$lt

Query string: foo=<20

Object:

{
  "foo": "<20"
}

Output:

{
  "foo": {
    "$lt": 20
  }
}

$lte

Query string: foo=<=20

Object:

{
  "foo": "<=20"
}

Output:

{
  "foo": {
    "$lte": 20
  }
}

$in

Query string: foo[]=a&foo[]=b

Object:

{
  "foo": [
    "a",
    "b"
  ]
}

Output:

{
  "foo": {
    "$in": [
      "a",
      "b"
    ]
  }
}

$nin

Query string: foo![]=a&foo![]=b

Object:

{
  "foo!": [
    "a",
    "b"
  ]
}

Output:

{
  "foo": {
    "$nin": [
      "a",
      "b"
    ]
  }
}

$or (1)

Query string: or[0][a]=>20&or[1][b]=$bar

Object:

{
  "or": [
    {
      "a": ">20"
    },
    {
      "b": "$bar"
    }
  ]
}

Output:

{
  "$or": [
    {
      "a": {
        "$gt": 20
      }
    },
    {
      "b": {
        "$regex": "bar",
        "$options": "i"
      }
    }
  ]
}

$or (2)

Object:

{
  "or": [
    {
      "a": "$foo"
    },
    {
      "b": [
        "n",
        "m"
      ]
    }
  ]
}

Output:

{
  "$or": [
    {
      "a": {
        "$options": "i",
        "$regex": "foo"
      }
    },
    {
      "b": {
        "$in": [
          "n",
          "m"
        ]
      }
    }
  ]
}

$and

Query string: and[0][a]=>20&and[1][b]=$bar

Object:

{
  "and": [
    {
      "a": ">20"
    },
    {
      "b": "$bar"
    }
  ]
}

Output:

{
  "$and": [
    {
      "a": {
        "$gt": 20
      }
    },
    {
      "b": {
        "$regex": "bar",
        "$options": "i"
      }
    }
  ]
}

$nor

Query string: nor[0][a]=>20&nor[1][b]=$bar

Object:

{
  "nor": [
    {
      "a": ">20"
    },
    {
      "b": "$bar"
    }
  ]
}

Output:

{
  "$nor": [
    {
      "a": {
        "$gt": 20
      }
    },
    {
      "b": {
        "$regex": "bar",
        "$options": "i"
      }
    }
  ]
}

$regex

Query string: foo=$bar

Object:

{
  "foo": "$bar"
}

Output:

{
  "foo": {
    "$regex": "bar",
    "$options": "i"
  }
}

date

Query string: foo=2023-6-7

Object:

{
  "foo": "2023-6-7"
}

Output:

{
  "foo": {
    "$eq": "<date object>"
  }
}

date

Query string: foo=<2023-6-7

Object:

{
  "foo": "<2023-6-7"
}

Output:

{
  "foo": {
    "$lt": "<date object>"
  }
}

Package Sidebar

Install

npm i q2m

Weekly Downloads

4

Version

1.4.1

License

MIT

Unpacked Size

15.9 kB

Total Files

8

Last publish

Collaborators

  • krasimir