Registry / serialization / bson-transpilers

bson-transpilers

JSON →
library3.3.5jsnpmunverified

Source-to-source compiler for BSON document expressions, converting between shell input and multiple output languages (Java, C#, Node.js, Python, Ruby, Go). Current stable version is 3.3.5. Developed by MongoDB for Compass, it uses ANTLR for parsing. The shell output is disabled and only intended for legacy mongo shell, not mongosh. Python support was removed but remains in an older commit. The library supports both Query and Pipeline modes, and can generate import statements automatically. It is the primary tool for converting MongoDB shell syntax to driver code in different languages, with error codes and line/column details for syntax errors.

npm install bson-transpilers
INSTALL
IMPORT
SIG · BSON-TRANSPILERS
B
bson-transpilers
serializationjavascriptv3.3.5
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.

transpiler
const transpiler = require('bson-transpilers');
import transpiler from 'bson-transpilers';
The package uses CommonJS and does not export ESM. The default export is the transpiler object. Named imports are not available.
transpiler.shell.java.compile
const result = transpiler.shell.java.compile(code);
transpiler.java.shell.compile(code) // wrong order
The API is transpiler[inputLang][outputLang].compile(string). Input and output languages are required. Common mistake is swapping them.
transpiler.shell.java.getImports
const imports = transpiler.shell.java.getImports('Query', true);
transpiler.shell.java.getImports(); // missing mode and driverSyntax arguments
getImports requires two arguments: mode ('Query' or 'Pipeline') and driverSyntax (boolean). Missing them leads to undefined behavior.
errors
} catch (error) { console.log(error.code, error.line, error.column); }
Error objects have custom properties: code (number), line (number), column (number), symbol (string). Not all errors have line/column.

Demonstrates basic usage: compile a BSON document from shell to Java, and generate necessary import statements.

const transpiler = require('bson-transpilers'); const inputLanguage = 'shell'; const outputLanguage = 'java'; const codeString = `{ item: "book", qty: Int32(10), tags: ["red", "blank"], dim_cm: [14, Int32("81")] }`; try { const compiled = transpiler[inputLanguage][outputLanguage].compile(codeString); console.log(compiled); // Output: new Document("item", "book").append("qty", 10).append("tags", Arrays.asList("red", "blank")).append("dim_cm", Arrays.asList(14L, 81))) const imports = transpiler[inputLanguage][outputLanguage].getImports('Query', true); console.log(imports); // Output: import java.util.Arrays; // import org.bson.Document; } catch (error) { console.error(`Error ${error.code}: ${error.message}`); if (error.line) console.error(`at line ${error.line}, column ${error.column}`); }
Debug
Known issues
deprecatedShell output is disabled and only compatible with legacy mongo shell, not mongosh.
fix
Use shell as input only; do not use output language 'shell'. Consider outputting JavaScript (node) for mongosh compatibility.
affects: >=1.0.0
breakingPython support has been removed from the package in recent versions.
fix
If you need Python output, use an older version (<=2.x) or reference the removed code at https://github.com/mongodb-js/compass/tree/80cf701e44cd966207f956fac69e8233861b1cd5/packages/bson-transpilers.
affects: >=3.0.0
gotchaThe getImports function requires two arguments (mode and driverSyntax). Omitting them will not throw but may return undefined or incorrect imports.
fix
Always call getImports with mode ('Query' or 'Pipeline') and driverSyntax (boolean).
affects: >=1.0.0
gotchaInput language is case-sensitive. Use 'shell', not 'Shell' or 'SHELL'.
fix
Ensure input language is lowercase and matches exactly: 'shell', 'javascript' (or 'node'?). Check documentation for supported values.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'bson-transpilers'
Package not installed or wrong import path.
fix
Run 'npm install bson-transpilers' and ensure you are using require (not import) in CommonJS context.
transpiler[input][output] is not a function
Invalid input or output language string, or the combination is not supported.
fix
Check that input is one of 'shell', 'javascript' and output is one of 'java', 'csharp', 'node', 'ruby', 'go'. Do not use 'python' (removed) or 'shell' (deprecated).
Error: line 1:0 mismatched input '{' expecting ...
Syntax error in the BSON expression – possibly missing braces, quotes, or invalid types.
fix
Validate the input code string. Use shell-compatible BSON literal syntax, e.g., { field: value } with proper Int32(), ObjectId(), etc.
TypeError: transpiler.shell.java.getImports is not a function
Output language 'java' might not be installed or supported in this version.
fix
Ensure the package is up to date (npm install bson-transpilers@latest) and that the output language is listed in supported outputs.
Upgrade
Version history
3.3.5latest on npm
Audit
Dependencies
antlr4requiredRuntime dependency for ANTLR-generated parser and lexer
Agent activity
20 hits · last 30 days
node
14
Amazon
1
OpenAI (training)
1
Resources
bson-transpilers — npm install bson-transpilers · libregistry