Registry / database / mongo-mask

mongo-mask

JSON →
library1.1.0jsnpmunverified

Converts a json-mask pattern (e.g., 'id,name,address(city)') into a MongoDB projection object for field inclusion/exclusion. v1.1.0 is the latest stable version; sporadic updates. Key differentiator: enables dynamic projection based on client query parameters, simplifying REST API field filtering for MongoDB queries. No runtime dependencies.

npm install mongo-mask
INSTALL
IMPORT
SIG · MONGO-MASK
M
mongo-mask
databasejavascriptv1.1.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.

default
import mongoMask from 'mongo-mask'
const mongoMask = require('mongo-mask')
package is ESM-only? Actually, package.json has no 'type' field and uses require in its own code, so CommonJS works. However, for ESM contexts, use default import.
default (CommonJS)
const mongoMask = require('mongo-mask')
import { mongoMask } from 'mongo-mask'
CommonJS require is the primary pattern; named import will result in undefined.
map option
mongoMask('id,name', { map: { id: '_id' } })
mongoMask('id,name', { map: { '_id': 'id' } })
The map is from json-mask field name to MongoDB field name, not reverse.

Shows how to convert a json-mask pattern to a MongoDB projection object, with optional field mapping.

const mongoMask = require('mongo-mask'); const pattern = 'id,name,address(city,zip)'; const projection = mongoMask(pattern); console.log(projection); // Output: { id: 1, name: 1, 'address.city': 1, 'address.zip': 1 } // With field mapping const map = { id: '_id' }; const mappedProjection = mongoMask(pattern, { map }); console.log(mappedProjection); // Output: { _id: 1, name: 1, 'address.city': 1, 'address.zip': 1 }
Debug
Known issues
gotchaInclusion/exclusion rules: All fields in projection must be either inclusions (1) or exclusions (0). Mixing 1 and 0 is not allowed except for _id exclusion.
fix
Ensure pattern selects only fields to include or only fields to exclude; do not mix unless for _id.
affects: >=1.0
gotchaNested fields use dot notation in projection (e.g., 'address.city'), but json-mask pattern uses parentheses (e.g., 'address(city)').
fix
The conversion handles parentheses to dot notation automatically.
affects: >=1.0
gotchaThe map option does not support nested field mapping (e.g., map nested directly).
fix
Map only top-level fields; for nested, map the parent and handle individually.
affects: >=1.0
Errors
Common errors & fixes
TypeError: mongoMask is not a function
Using named import instead of default import or require.
fix
Use `const mongoMask = require('mongo-mask')` or `import mongoMask from 'mongo-mask'`.
MongoError: Cannot do inclusion on field ... in exclusion projection
Mixing inclusion and exclusion fields in the same projection.
fix
Ensure pattern includes only fields to include (e.g., 'a,b,c') or only fields to exclude (e.g., '-a,-b,-c'), not both.
Pattern invalid: unexpected token
Malformed json-mask pattern, e.g., mismatched parentheses.
fix
Verify pattern syntax: e.g., 'field1,field2(nested)' is valid; 'field1,field2(' is invalid.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
mongo-mask — npm install mongo-mask · libregistry