query-collection

1.0.13 • Public • Published

query-collection

My goal is to create a simple but powerful way to search within a collection of object using a query that relflects MongoDB syntax.

Installation

npm i query-collection

then:

import { find } from "query-collection";

Implementation

Comparison operators

  • [x] $eq
  • [x] $gt
  • [x] $gte
  • [x] $in
  • [x] $lt
  • [x] $lte
  • [x] $ne
  • [x] $nin

Logical operators

  • [x] $or
  • [x] $and

Collection functions

  • [x] find

Usage

simplest query:

const query = {
    text: "string value"
}

implicit $and on multiple field:

const query = {
    text: "string value",
    total: 187,
    isRed: false
}

Comparison

$eq

const query = {
        text: {
          $eq: "strict equal string value"
        }
      };

$gt

const query = {
        total: {
          $gt: 47
        }
      };

$gte

const query = {
        total: {
          $gte: 47
        }
      };

$in

const query = {
        text: {
          $in: ["strict value A", "strict value B", "strict value C"]
        }
      };

$lt

const query = {
        total: {
          $lt: 47
        }
      };

$lte

const query = {
        total: {
          $lte: 60
        }
      };

$ne

const query = {
        text: {
          $ne: "string value"
        }
      };

$nin

const query = {
        total: {
          $nin: [47, 50, 90]
        }
      };

Logical

$or

const query = {
        $or: [
          {
            total: {
              $gte: 50
            }
          },
          {
            text: {
              $regex: /lue/i
            }
          },
          { isRed: true }
        ]
      };

$and

const query = {
        $and: [
          {
            total: {
              $in: [50, 92, 46]
            }
          },
          {
            text: {
              $regex: /lue/i
            }
          },
          { isRed: true }
        ]
      };

collection functions

find

const peopleArray = [
    {

    },
    {

    }
]

find(peopleArray, query)

Package Sidebar

Install

npm i query-collection

Weekly Downloads

1

Version

1.0.13

License

ISC

Unpacked Size

11.7 kB

Total Files

5

Last publish

Collaborators

  • learningprocesss