1.0.5 • Published 8 months ago

mongo-query-parse-filter v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

Mongo Query Parse Filter

Lightweight package that allows you to easily convert complex, human-readable query filters into MongoDB-compatible query syntax, supporting logical operations like AND, OR, NOT, IN, NIN and various comparison operators


Table of Contents

  1. Installation
  2. Usage
  3. Example

Installation

To install the package, use npm:

npm i mongo-query-parse-filter

Usage

const { MongoQuery } = require('mongo-query-parse-filter');

const mongoQuery = new MongoQuery();

const query = mongoQuery.buildQuery('(email eq "jhon@example.com")')

console.log(query)

Result

{
    "email": { "$eq": "jhon@example.com" }
}

Example

eq,neq,gt,gte,lt,lte,regex: (email regex "(?i)@example.com$")

{
    "email": { "$regex": "(?i)@example.com$" }
}

OR/AND: (email eq "jhon@example.com") or (username eq "alice")

{
    "$or": [
        {
            "email": { "$eq": "jhon@example.com" }
        },
        {
            "username": { "$eq": "alice" }
        }
    ]
}

NOT: (not ((department eq "Marketing") or (department eq "Sales")))

to find all employees who are not either in the Marketing or Sales departments

{
    "$not": {
        "$or": [
            {
                "department": { "$eq": "Marketing" }
            },
            {
                "department": { "$eq": "Sales" }
            }
        ]
    }
}

IN/NIN: (email in "'alice@example.com','jhon@example.com'")

{
    "email": { "$in": [ "alice@example.com", "jhon@example.com"] }
}
1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago