Goal is to create a simple but powerful way to search within a collection of object using a query that relflects MongoDB syntax.
npm i query-collection
then:
import { find } from "query-collection";
- $eq
- $gt
- $gte
- $in
- $lt
- $lte
- $ne
- $nin
- $or
- $and
- find
simplest query:
const query = {
text: "string value"
}
implicit $and on multiple field:
const query = {
text: "string value",
total: 187,
isRed: false
}
const query = {
text: {
$eq: "strict equal string value"
}
};
const query = {
total: {
$gt: 47
}
};
const query = {
total: {
$gte: 47
}
};
const query = {
text: {
$in: ["strict value A", "strict value B", "strict value C"]
}
};
const query = {
total: {
$lt: 47
}
};
const query = {
total: {
$lte: 60
}
};
const query = {
text: {
$ne: "string value"
}
};
const query = {
total: {
$nin: [47, 50, 90]
}
};
const query = {
$or: [
{
total: {
$gte: 50
}
},
{
text: {
$regex: /lue/i
}
},
{ isRed: true }
]
};
const query = {
$and: [
{
total: {
$in: [50, 92, 46]
}
},
{
text: {
$regex: /lue/i
}
},
{ isRed: true }
]
};
const peopleArray = [
{
},
{
}
]
find(peopleArray, query)