monk-middleware-fields
raw JSON → 0.2.0 verified Sat Apr 25 auth: no javascript maintenance
A middleware for the Monk MongoDB library that parses and filters document fields. Version 0.2.0 is the latest stable release. This package is part of the Monk middleware ecosystem and extracts the field selection logic into a separate module, allowing reusable field filtering in queries and updates. Unlike manual field projection, monk-middleware-fields provides a declarative way to include or exclude fields consistently across operations. It is designed to work with Monk versions that support middleware (v5+). No active development observed; project appears unmaintained.
Common errors
error TypeError: fields is not a function ↓
cause Using named import instead of default import in ESM.
fix
Use 'import fields from 'monk-middleware-fields'' instead of 'import { fields } from 'monk-middleware-fields''.
error Error: Cannot find module 'monk-middleware-fields' ↓
cause Package not installed or not in node_modules.
fix
Run 'npm install monk-middleware-fields'.
error fields option ignored in query results ↓
cause Middleware not applied to the collection instance.
fix
Pass { middlewares: [fields()] } when creating the collection.
Warnings
deprecated Package is no longer actively maintained; last release 0.2.0 in 2016. ↓
fix Consider using built-in MongoDB projection in Monk directly or find an alternative.
gotcha The fields option in queries may not work as expected if not passed to the middleware. ↓
fix Ensure you instantiate the collection with the middleware: db.get('collection', { middlewares: [fields()] }).
gotcha The fields middleware does not support nested field paths like 'address.city' in some versions. ↓
fix Upgrade to 0.2.0 or use dot notation manually.
breaking Monk 7.0.0 changed internal MongoDB driver; monk-middleware-fields may not be fully compatible. ↓
fix Test with your Monk version; consider using Monk's built-in projection.
Install
npm install monk-middleware-fields yarn add monk-middleware-fields pnpm add monk-middleware-fields Imports
- fields wrong
const { fields } = require('monk-middleware-fields')correctimport fields from 'monk-middleware-fields' - fields wrong
const fields = require('monk-middleware-fields').defaultcorrectconst fields = require('monk-middleware-fields') - Monk wrong
import { Monk } from 'monk'correctimport monk from 'monk'
Quickstart
import monk from 'monk';
import fields from 'monk-middleware-fields';
const db = monk('localhost/mydb');
const users = db.get('users', { middlewares: [fields()] });
// Insert a user
await users.insert({ name: 'John', email: 'john@example.com', age: 30 });
// Query with fields: only name and email
const result = await users.findOne({}, { fields: { name: 1, email: 1 } });
console.log(result); // { _id: ..., name: 'John', email: 'john@example.com' }
// Or exclude fields
const result2 = await users.findOne({}, { fields: { age: 0 } });
console.log(result2); // { _id: ..., name: 'John', email: 'john@example.com' }