Registry / database / mongo-dot-notation

mongo-dot-notation

JSON →
library3.1.1jsnpmunverified

3.1.1. Transform plain JavaScript objects into MongoDB update instruction objects with dot-notation paths. Supports all standard MongoDB update operators ($set, $inc, $push, $unset, $currentDate, etc.), array flattening, and positional operators ($, $[], $[identifier]). No runtime dependency on the mongo driver. Written in TypeScript with bundled type definitions. Frequently updated with CI and high test coverage.

npm install mongo-dot-notation
INSTALL
IMPORT
SIG · MONGO-DOT-NOTATION
M
mongo-dot-notation
databasejavascriptv3.1.1
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.

flatten
import { flatten } from 'mongo-dot-notation'
const flatten = require('mongo-dot-notation')
The package is ESM-only since v3. Use named import for CommonJS via `const { flatten } = require('mongo-dot-notation')`.
$inc
import { $inc } from 'mongo-dot-notation'
import $inc from 'mongo-dot-notation'
$inc is a named export, not a default export.
$push
import { $push } from 'mongo-dot-notation'
import { push } from 'mongo-dot-notation'
The operator exports include the dollar sign in the name.

Demonstrates using flatten with operators $inc, $push, and $unset.

import { flatten, $inc, $push, $unset } from 'mongo-dot-notation'; const updateDoc = flatten({ name: 'Bob', age: $inc(1), hobbies: $push('chess'), tempField: $unset(), }); console.log(updateDoc); // Output: // { // $set: { name: 'Bob' }, // $inc: { age: 1 }, // $push: { hobbies: 'chess' }, // $unset: { tempField: '' } // }
Debug
Known issues
breakingflatten now uses ES module exports; require() no longer works with default export.
fix
Use `import { flatten } from 'mongo-dot-notation'` or CommonJS `const { flatten } = require('mongo-dot-notation')`.
affects: >=3.0.0
deprecatedThe `object` option in flatten is deprecated; use `{ array: true }` to flatten arrays.
fix
Use `{ array: true }` instead of `{ object: true }`.
affects: >=2.0.0
gotchaNested arrays without the `{ array: true }` option are treated as plain objects.
fix
Pass `{ array: true }` to flatten to convert array indices into dot notation.
affects: >=1.0.0
gotchaOperator functions must be called before flatten; passing raw numbers/strings to $inc etc. will not work.
fix
Use `$inc(amount)` instead of `$inc` alone.
affects: >=1.0.0
gotchaThe `$` positional operator only works when the flattened object is used in an updateOne query with a matching array filter.
fix
Use nested positional operator syntax like `$('field')` or `$('[].field')`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: flatten is not a function
Using `import flatten from 'mongo-dot-notation'` in an ESM context—flatten is a named export.
fix
Change to `import { flatten } from 'mongo-dot-notation'`.
Error: Invalid operator: $inc
Calling `$inc` without parentheses or passing a non-function value.
fix
Use `$inc(amount)` to create a valid operator object.
MongoError: Unrecognized field '$set'
Using flatten output directly in find() or aggregate() instead of updateOne/updateMany.
fix
Use `flatten()` output only with MongoDB update operations (updateOne, updateMany, findOneAndUpdate, etc.).
Upgrade
Version history
3.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
mongo-dot-notation — npm install mongo-dot-notation · libregistry