1.0.13 • Published 4 years ago

query-collection v1.0.13

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

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

  • $eq
  • $gt
  • $gte
  • $in
  • $lt
  • $lte
  • $ne
  • $nin

Logical operators

  • $or
  • $and

Collection functions

  • 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)
1.0.13

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago