1.0.13 • Published 4 years ago
query-collection v1.0.13
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)