1.4.1 • Published 11 months ago

q2m v1.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

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>"
  }
}
1.4.1

11 months ago

1.4.0

11 months ago

1.3.1

11 months ago

1.2.5

11 months ago

1.2.4

11 months ago

1.2.3

11 months ago

1.2.2

11 months ago

1.2.1

11 months ago

1.2.0

11 months ago

1.1.8

11 months ago

1.1.7

11 months ago

1.1.6

11 months ago

1.1.5

11 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago

0.1.0

11 months ago