eslint-plugin-mongodb

raw JSON →
1.0.0 verified Fri May 01 auth: no javascript

ESLint rules for the Node.js MongoDB native driver 2.0 syntax and best practices. Designed to catch common errors and deprecated patterns in MongoDB query calls (find, insert, update, remove). This v1.0.0 release is the first stable version. It provides 8 rules covering argument validation, deprecated method detection, and update operation checks. Integrates as an ESLint plugin using shared settings to customize call patterns via regex. Release cadence is low; no recent updates. Key differentiator: focused specifically on Node driver 2.0 API, unlike generic MongoDB linters.

error Cannot read property 'collection' of undefined
cause Rule expects db.collection() pattern but db is not defined in scope
fix
Ensure MongoDB connection object is named 'db' or adjust callPatterns in settings
error Parsing error: Unexpected token /
cause Using regex literal in .eslintrc instead of string pattern
fix
Wrap patterns in quotes: '(\.|^)db\.collection\([^\)]+\)\.(find|findOne|)$'
error ESLint configuration is invalid: "rules": ["mongodb/no-replace": 0] is not an array
cause Using array syntax instead of object syntax in .eslintrc
fix
Use object syntax: 'rules: { "mongodb/no-replace": 0 }'
breaking All rules require prefix 'mongodb/' when configured
fix Use 'rules: { "mongodb/no-replace": 2 }' instead of 'rules: { "no-replace": 2 }'
gotcha Shared settings callPatterns are regex strings; must double-escape backslashes
fix Use '\\.' for a literal dot instead of '.'
gotcha Plugin does not work with MongoDB driver 3.0+ methods (e.g., collection.bulkWrite)
fix Customize callPatterns in settings to match driver 3.0 API, or use a different linter
deprecated Driver 2.0 is deprecated; consider using driver 3.0+
fix Upgrade to mongodb driver >=3.0 and adjust callPatterns accordingly
npm install eslint-plugin-mongodb
yarn add eslint-plugin-mongodb
pnpm add eslint-plugin-mongodb

Example .eslintrc configuration enabling all mongodb plugin rules with default settings.

// Install: npm install --save-dev eslint eslint-plugin-mongodb
// .eslintrc.yml
plugins:
  - mongodb
rules:
  mongodb/check-insert-calls: 2
  mongodb/check-query-calls: 2
  mongodb/check-update-calls: 2
  mongodb/check-remove-calls: 2
  mongodb/check-deprecated-calls: 2
  mongodb/no-replace: 1
  mongodb/check-rename-updates: 2
  mongodb/check-unset-updates: 2
settings:
  mongodb:
    callPatterns:
      query:
        - "(\\.|^)db\\.collection\\([^\\)]+\\)\\.(find|findOne|)$"
      update:
        - "(\\.|^)db\\.collection\\([^\\)]+\\)\\.(findOneAndUpdate|updateOne|updateMany)$"
      insert:
        - "(\\.|^)db\\.collection\\([^\\)]+\\)\\.(insertOne|insertMany)$"
      remove:
        - "(\\.|^)db\\.collection\\([^\\)]+\\)\\.(findOneAndDelete|deleteOne|deleteMany)$"
      deprecated:
        - "(\\.|^)db\\.collection\\([^\\)]+\\)\\.(remove|update|findAndModify|ensureIndex|findAndRemove|insert|dropAllIndexes)$"