Registry / testing / unified-mongo-filter

unified-mongo-filter

JSON →
library0.4.0jsnpmunverified

A lightweight utility for applying MongoDB-like query filters (e.g., $eq, $gte, $in, $regex) to plain JavaScript objects. Version 0.4.0 is current, with stable API and low release cadence. Key differentiator: no database dependency, pure in-memory filtering. Supports nested fields, dot notation, and most MongoDB operators.

npm install unified-mongo-filter
INSTALL
IMPORT
SIG · UNIFIED-MONGO-FILT
U
unified-mongo-filter
testingjavascriptv0.4.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

matches
✓ import { matches } from 'unified-mongo-filter'
✗ const { matches } = require('unified-mongo-filter')
ESM-only; CommonJS require will fail. Use dynamic import if needed.
createValidator
✓ import { createValidator } from 'unified-mongo-filter'
✗ import createValidator from 'unified-mongo-filter'
createValidator is a named export, not default.
default
✓ import unifiedMongoFilter from 'unified-mongo-filter'
✗ import { default as unifiedMongoFilter } from 'unified-mongo-filter'
Library does not export a default; this import will be undefined.

Filters an array of objects using a MongoDB-style query with $or and $gte operators.

import { matches } from 'unified-mongo-filter'; const filter = { $or: [{ age: { $gte: 18 } }, { role: 'admin' }] }; const users = [ { name: 'Alice', age: 20, role: 'user' }, { name: 'Bob', age: 17, role: 'admin' }, { name: 'Charlie', age: 15, role: 'user' } ]; const filtered = users.filter(user => matches(user, filter)); console.log(filtered); // [{ name: 'Alice', age: 20, role: 'user' }, { name: 'Bob', age: 17, role: 'admin' }]
Debug
Known issues
gotchaOperator $regex expects a RegExp object or string; flags must be embedded in string like '/pattern/flags'.
fix
Use new RegExp('pattern', 'flags') or string '/pattern/flags'.
affects: >=0.0.0
gotchaNested field access uses dot notation; 'a.b' will not match {a: {b: 1}} if value is array?
fix
Ensure filter keys use dot path for nested fields, e.g., 'address.city'.
affects: >=0.0.0
gotchaUsing $elemMatch with non-array field returns undefined?
fix
Validate field is array before applying $elemMatch.
affects: >=0.0.0
gotchaNull comparison: {$eq: null} works but {$ne: null} may not match missing keys.
fix
Use {$exists: false} for missing keys, not $ne: null.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: matches is not a function
CommonJS require used; ESM-only package.
fix
Use import { matches } from 'unified-mongo-filter' or dynamic import.
Uncaught TypeError: Cannot read properties of undefined (reading '$gte')
Filter object malformed; nested operator not in expression format.
fix
Ensure filter values are objects with operator keys, e.g., { age: { $gte: 18 } }.
Error: Invalid filter operator: $custom
Unsupported custom operator used.
fix
Use only supported operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $regex, $exists, $type, $elemMatch, $and, $or, $not, $nor, $where, $expr.
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
14
Resources
unified-mongo-filter — npm install unified-mongo-filter · libregistry