Querying
How to use
where — operator engine
Three call signatures:
// 1. Key + value (implicit "=" equality)c.where("active", true);
// 2. Key + operator + valuec.where("age", ">", 18);c.where("name", "like", "ada"); // case-insensitive substringc.where("name", "starts with", "A");c.where("name", "ends with", "a");c.where("role", "in", ["admin", "mod"]);c.where("age", "between", [20, 30]); // inclusivec.where("score", "between", [0, 100]);
// 3. Operator only (no key — tests the item itself for primitives)c.where(">=", 18); // items that are numbers >= 18RegExp shorthand
Pass a RegExp as the value and the key check becomes a regex test:
c.where("name", /^A/);Existence vs null vs undefined
| Operator | Matches |
|---|---|
"exists" / "not exists" | key is present / absent on the item object |
"null" / "is null" | value is strictly null |
"undefined" / "is undefined" | value is strictly undefined |
"empty" / "is empty" | value is empty (via @mongez/supportive-is) |
c.where("nickname", "exists");c.where("deletedAt", "is null");c.where("config", "is not empty");Type checks
c.where("age", "is", "number"); // typeof itemValue === "number"c.where("handler", "instanceof", MyClass); // itemValue instanceof MyClassc.where("flag", "is true");c.where("flag", "is false");Convenience where* shorthand methods
c.whereIn("status", ["active", "pending"]);c.whereBetween("age", [20, 30]);c.whereNotBetween("score", [0, 50]);c.whereNot("status", "banned");c.whereNull("deletedAt");c.whereNotNull("email");c.whereEmpty("tags");c.whereNotEmpty("tags");c.whereExists("metadata");c.whereNotExists("legacyId");filter — callback-based
c.filter(item => item.score > 90 && item.verified);reject / except
Returns items for which the callback returns false (inverse of filter):
c.reject(item => item.banned);c.except(item => item.role === "guest"); // aliasnot — exclude a specific primitive value
collect([1, 2, 3, null]).not(null); // [1, 2, 3]find — single item by callback
const user = c.find(u => u.id === 42); // returns item or undefinedfirst / last
c.first(); // first item or undefinedc.last(); // last item or undefinedfirstWhere / lastWhere
Same signature as where, but returns the single matched item instead of a collection:
const admin = c.firstWhere("role", "admin");const newest = c.lastWhere("status", "active");const over25 = c.firstWhere("age", ">", 25);Key details / Pitfalls
wherealways returns a new collection — it never mutates. Chain as many as needed.- All operators are string literals defined in
src/types.ts. Passing an unrecognised operator returns an empty collection (falls through todefault: return false). - Chained
wherecalls are AND logic — each narrows the previous result. For OR logic, usefilterwith||or union twowhereresults. - Keys support dot notation:
c.where("address.city", "London"). - If items have a
.get(key)method it is used for all keyed lookups — collection integrates seamlessly with model classes. whereInwith a single array argument (no key) tests the item itself:c.whereIn([1, 2, 3])keeps primitive items whose value is in the list.