Registry / database / jsonpatch-to-mongodb

jsonpatch-to-mongodb

JSON →
library1.0.0jsnpmunverified

Convert JSON Patch (RFC 6902) operations into MongoDB update documents. Version 1.0.0 provides a single function that translates an array of JSON patch operations (add, remove, replace, move, copy, test) into a MongoDB-compatible update object with $set, $unset, $push, $pull, etc. No release cadence specified; actively maintained by MongoDB.js team. Differentiators: lightweight, no external dependencies, designed specifically for MongoDB update generation.

npm install jsonpatch-to-mongodb
INSTALL
IMPORT
SIG · JSONPATCH-TO-MONGO
J
jsonpatch-to-mongodb
databasejavascriptv1.0.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.

toMongodb
import toMongodb from 'jsonpatch-to-mongodb'
const toMongodb = require('jsonpatch-to-mongodb')
Package does not export TypeScript types; use @types/jsonpatch-to-mongodb if available.
toMongodb
const toMongodb = require('jsonpatch-to-mongodb')
const toMongodb = require('jsonpatch-to-mongodb').default
CommonJS require works directly; no .default needed.
toMongodb
import { toMongodb } from 'jsonpatch-to-mongodb'
import toMongodb from 'jsonpatch-to-mongodb'
Package does not export named export; only default export.

Converts two JSON patch operations (replace and add) into a MongoDB update object with $set and $push operators.

import toMongodb from 'jsonpatch-to-mongodb'; const patches = [ { op: 'replace', path: '/name', value: 'dave' }, { op: 'add', path: '/emails/-', value: 'dave@example.com' } ]; const update = toMongodb(patches); console.log(update); // { '$set': { name: 'dave' }, '$push': { emails: 'dave@example.com' } }
Debug
Known issues
gotchaPatch operations are applied sequentially; order matters and can produce unexpected results if not intended.
fix
Ensure patches are in the correct order (e.g., remove before replace at same path).
affects: <=1.0.0
gotchaThe 'test' operation is not supported; it will throw an error if included.
fix
Remove test operations before passing to toMongodb, or handle them separately.
affects: <=1.0.0
gotchaPaths must follow JSON Pointer syntax (e.g., '/foo/bar'). Invalid paths (e.g., missing leading '/') may cause errors or incorrect updates.
fix
Validate paths with a JSON Pointer library or ensure they start with '/'.
affects: <=1.0.0
deprecatedThe package does not provide TypeScript definitions; TypeScript users must create custom type declarations.
fix
Add a declaration file: declare module 'jsonpatch-to-mongodb' { function toMongodb(patches: any[]): any; export default toMongodb; }
affects: <=1.0.0
Errors
Common errors & fixes
TypeError: toMongodb is not a function
Using named import instead of default import, or using require().default on CommonJS.
fix
Use default import: import toMongodb from 'jsonpatch-to-mongodb' or const toMongodb = require('jsonpatch-to-mongodb').
Error: Unsupported operation: test
The 'test' operation is not implemented in jsonpatch-to-mongodb.
fix
Remove test operations from patch array before calling toMongodb, or implement custom handling.
TypeError: Cannot read property 'split' of undefined
Patch object missing 'path' property or path is not a string.
fix
Ensure each patch has a valid 'path' string property (e.g., '/name').
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
jsonpatch-to-mongodb — npm install jsonpatch-to-mongodb · libregistry