bson-transpilers

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

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.

error Cannot find module 'bson-transpilers'
cause Package not installed or wrong import path.
fix
Run 'npm install bson-transpilers' and ensure you are using require (not import) in CommonJS context.
error transpiler[input][output] is not a function
cause 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 Error: line 1:0 mismatched input '{' expecting ...
cause 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.
error TypeError: transpiler.shell.java.getImports is not a function
cause 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.
deprecated Shell 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.
breaking Python 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.
gotcha The 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).
gotcha Input 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.
npm install bson-transpilers
yarn add bson-transpilers
pnpm add bson-transpilers

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}`);
}