### Install mongosh globally Source: https://github.com/mongodb-js/mongosh/blob/main/packages/mongosh/README.md Use this command to install the `mongosh` binary globally on your system. ```bash npm i -g mongosh ``` -------------------------------- ### Example User Input in CLI REPL Source: https://github.com/mongodb-js/mongosh/blob/main/docs/cli-code-evaluation.md This is an example of user input that would be processed by the CLI REPL. ```javascript db.coll.find({}) ``` -------------------------------- ### Start the CLI via npm Source: https://github.com/mongodb-js/mongosh/blob/main/README.md Compile and start the Mongosh CLI. Accepts connection strings and CLI arguments. ```shell npm run start <... connection string, CLI args, etc.> ``` -------------------------------- ### Install Dependencies and Compile CLI Source: https://github.com/mongodb-js/mongosh/blob/main/README.md Run these commands to bootstrap the project and compile the CLI package. ```shell npm run bootstrap npm run compile-cli ``` -------------------------------- ### Install @mongosh/logging Package Source: https://github.com/mongodb-js/mongosh/blob/main/packages/logging/README.md Install the @mongosh/logging package as a dependency using npm. ```shell npm install -S @mongosh/logging ``` -------------------------------- ### Example Linux-Style Command in CLI REPL Source: https://github.com/mongodb-js/mongosh/blob/main/docs/cli-code-evaluation.md This is an example of a Linux-style command that is detected and processed by the Shell Evaluator. ```shell show dbs ``` -------------------------------- ### Initialize ElectronRuntime Source: https://github.com/mongodb-js/mongosh/blob/main/packages/browser-runtime-electron/README.md Import and instantiate ElectronRuntime with a service provider. This is the basic setup for using the runtime. ```javascript import { ElectronRuntime } from 'mongosh-browser-repl'; const runtime = new ElectronRuntime(serviceProvider); ``` -------------------------------- ### Mongosh DB Address Examples Source: https://github.com/mongodb-js/mongosh/blob/main/README.md Examples demonstrating how to specify the database address for mongosh connections, including local and remote connections with and without specified ports. ```shell foo Foo database on local machine 192.168.0.5/foo Foo database on 192.168.0.5 machine 192.168.0.5:9999/foo Foo database on 192.168.0.5 machine on port 9999 mongodb://192.168.0.5:9999/foo Connection string URI can also be used ``` -------------------------------- ### Install @mongosh/cli-repl package Source: https://github.com/mongodb-js/mongosh/blob/main/packages/cli-repl/README.md Installs the @mongosh/cli-repl package as a dependency for local development. ```shell npm install --save @mongosh/cli-repl ``` -------------------------------- ### Display Simple String Document Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/src/test/resources/cursor/print.expected.txt Example output showing a document containing a simple string. ```json [ { "0": "Hello, world!" } ] ``` -------------------------------- ### Start the CLI from cli-repl package Source: https://github.com/mongodb-js/mongosh/blob/main/README.md Start the Mongosh CLI directly from the cli-repl package after ensuring other dependencies are compiled. ```shell npm run -w @mongosh/cli-repl start <... connection string, CLI args, etc.> ``` -------------------------------- ### Display Documents with String and Object Values Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/src/test/resources/cursor/print.expected.txt Example output showing documents with string and object values. ```json [ { "0": "my document", "1": { "_id": , "a": 1 } }, { "0": "my document", "1": { "_id": , "a": 2 } }, { "0": "my document", "1": { "_id": , "a": 3 } } ] ``` -------------------------------- ### Document Array Example Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/src/test/resources/collection/updateOnePipeline.expected.txt Shows an array of documents, each with an _id and potentially other fields. ```javascript [ { "_id": , "b": 1 }, { "_id": , "a": 1 }, { "_id": , "a": 1 } ] ``` -------------------------------- ### Install and Use ndjson Pretty-Printer for Mongosh Logs Source: https://github.com/mongodb-js/mongosh/wiki/Logging Install the pino-colada package globally to pretty-print ndjson logs. Then, tail the mongosh log file for a specific session and pipe it to pino-colada for readable output during troubleshooting. ```shell npm i -g pino-colada@1.6.1 ``` ```shell tail -f ~/.mongodb/mongosh/ef0b8c25a93342b91238e220_log | pino-colada ``` -------------------------------- ### Start mongosh with a specific database Source: https://github.com/mongodb-js/mongosh/blob/main/README.md Connect to a MongoDB instance using a provided connection string and specify the database to use. This is useful for targeting a particular database upon startup. ```bash $ mongosh mongodb://192.168.0.5:9999/ships ``` -------------------------------- ### Display Documents with Numeric Values Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/src/test/resources/cursor/print.expected.txt Example output showing documents with numeric values. ```json [ { "0": { "_id": , "a": 1 } }, { "0": { "_id": , "a": 2 } }, { "0": { "_id": , "a": 3 } } ] ``` -------------------------------- ### mongosh Command-Line Usage Source: https://github.com/mongodb-js/mongosh/wiki/Home Use this command to start the mongosh shell, specifying connection options, authentication, TLS settings, and database addresses. Files ending in .js can be executed directly. ```shell $ mongosh [options] [db address] ``` ```shell $ mongosh mongodb://192.168.0.5:9999/ships ``` -------------------------------- ### DBRefResult Structure Example Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/src/test/resources/literal/DBRef.expected.txt Illustrates the basic structure of a DBRefResult object, containing a reference namespace and an object ID. ```json { "$ref" : "namespace", "$id" : "oid" } ``` -------------------------------- ### Run mongosh without full installation Source: https://github.com/mongodb-js/mongosh/blob/main/README.md Use this command if you have npm installed and want to run mongosh without a complete installation process. ```bash npx mongosh ``` -------------------------------- ### Initialize Shell BSON Helpers and Evaluate Code Source: https://github.com/mongodb-js/mongosh/blob/main/packages/shell-bson/README.md Import necessary modules, configure shell-compatible BSON pretty-printing, construct BSON helpers, and evaluate a string containing BSON types within a new context. ```javascript import { constructShellBson, makePrintableBson } from '@mongosh/shell-bson'; import * as bson from 'bson'; import * as vm from 'vm'; // Optional: Get shell-compatible pretty-printing for BSON objects // when used with Node.js's custom inspect() functionality makePrintableBson(bson); // Create Shell-like BSON helpers const shellBson = constructShellBson({ bsonLibrary: bson, printWarning: console.warn, }); // Evaluate code against Shell-like BSON helpers const document = vm.runInNewContext( '({ uuid: UUID(), dec128: NumberDecimal("12.34") })', { ...shellBson } ); ``` -------------------------------- ### Install @mongosh/editor Package Source: https://github.com/mongodb-js/mongosh/blob/main/packages/editor/README.md Use npm to install the @mongosh/editor package as a dependency. ```shell npm install -S @mongosh/editor ``` -------------------------------- ### Install @mongosh/js-multiline-to-singleline Package Source: https://github.com/mongodb-js/mongosh/blob/main/packages/js-multiline-to-singleline/README.md Install the package using npm. This is a dependency for the MongoDB Shell. ```shell npm install -S @mongosh/js-multiline-to-singleline ``` -------------------------------- ### Create Client Bundles Source: https://github.com/mongodb-js/mongosh/blob/main/packages/testing/certificates/README.md Generate combined files containing the client certificate and its private key. Two versions are created: one with an unencrypted key and one with an encrypted key. ```bash cat client.pem client.key > client.bundle.pem ``` ```bash cat client.pem client.encrypted.key > client.bundle.encrypted.pem ``` -------------------------------- ### Install `@mongosh/errors` Source: https://github.com/mongodb-js/mongosh/blob/main/packages/autocomplete/README.md Install the `@mongosh/errors` package using npm. This is a dependency for the autocompletion functionality. ```shell npm install -S @mongosh/errors ``` -------------------------------- ### Create PFX Bundle for Client Source: https://github.com/mongodb-js/mongosh/blob/main/packages/testing/certificates/README.md Convert the client certificate and key bundle into a PKCS#12 (PFX) file format. This format is commonly used for importing certificates into various applications and systems. ```bash openssl pkcs12 -inkey client.bundle.pem -in client.bundle.pem -export -out client.bundle.pfx ``` -------------------------------- ### Document Retrieval Example Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/src/test/resources/collection/updateOneUpsert.expected.txt An example of a document retrieved from a MongoDB collection using mongosh, showing its structure and content. ```javascript [ { "_id": , "a": 1, "b": 1 } ] ``` -------------------------------- ### Build JAR File with Gradle Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/README.md Command to create a JAR file for the library using Gradle. The output JAR will be located in the `build/libs` directory. ```bash ./gradlew jar ``` -------------------------------- ### Execute MongoDB Shell Commands in Java Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/README.md Demonstrates how to initialize the MongoDB Shell for JVM, execute a find command on the 'companies' collection, and iterate over the results. Ensure MongoDB is running and accessible via the specified connection string. ```java import com.mongodb.ConnectionString; import com.mongodb.MongoClientSettings; import com.mongodb.client.MongoClients; import com.mongodb.mongosh.MongoShell; import com.mongodb.mongosh.result.Cursor; import com.mongodb.mongosh.result.CursorResult; import org.bson.Document; /* ... */ MongoClientSettings settings = MongoClientSettings.builder() .applyConnectionString(new ConnectionString("mongodb://localhost:27017")) .build(); try (MongoShell shell = new MongoShell(MongoClients.create(settings))) { shell.eval("use admin"); CursorResult result = (CursorResult) shell.eval("db.companies.find()"); Cursor cursor = result.getValue(); while (cursor.hasNext()) { Document doc = cursor.next(); System.out.println(doc); } } ``` -------------------------------- ### Build mongosh MSI Package Source: https://github.com/mongodb-js/mongosh/blob/main/packaging/msi-template/README.md Use these commands to build the Windows Installer (MSI) package for mongosh. Ensure WiX toolset is installed and configured in your environment variables. ```shell "%WIX%\bin\candle.exe" -out obj\Release\ -arch x64 -ext "%WIX%\bin\WixUIExtension.dll" MongoshUI.wxs Product.wxs ``` ```shell "%WIX%\bin\Light.exe" -out bin\Release\windows.msi -cultures:en-US -ext "%WIX%\bin\WixUIExtension.dll" -loc MongoshUI.en-US.wxl obj\Release\MongoshUI.wixobj obj\Release\Product.wixobj ``` -------------------------------- ### Get Autocompletions Source: https://github.com/mongodb-js/mongosh/blob/main/packages/autocomplete/README.md Use this function to get autocompletion suggestions based on the server version and the current input line. It returns an array of possible completions and the input line itself. ```javascript const autocomplete = require('@mongosh/autocomplete'); const serverVersion = '4.4.0'; const line = 'db.coll.fin'; const completions = autocomplete(serverVersion, line); if (!completions || !completions.length) { return []; } const entries = completions[0].map((completion) => { return { completion, }; }); ``` ```javascript const autocomplete = require('@mongosh/autocomplete'); const serverVersion = '4.4.0'; const line = 'db.coll.re'; const completions = autocomplete(serverVersion, line); // returns: // [ // [ 'db.coll.renameCollection', 'db.coll.replaceOne', 'db.coll.reIndex' ], // 'db.coll.re' // ] ``` -------------------------------- ### Usage in Compass with Shell Component Source: https://github.com/mongodb-js/mongosh/blob/main/packages/browser-runtime-electron/README.md Demonstrates integrating ElectronRuntime with the Shell component in a Compass environment. Requires connecting to a service provider first. ```javascript import { Shell, ElectronRuntime } from 'mongosh-browser-repl'; const runtime = new ElectronRuntime( await CompassServiceProvider.connect(...) ); function MyShell(props) { return ; } ``` -------------------------------- ### Compile Executable and Package for Debian Source: https://github.com/mongodb-js/mongosh/blob/main/README.md Compile the standalone executable and then package it for Debian distribution. ```shell npm run compile-exec npm run evergreen-release package -- --build-variant=deb-x64 ``` -------------------------------- ### Array of Documents Example Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/src/test/resources/collection/replaceOneUpsert.expected.txt This displays an array of documents, each with an _id and a field 'a'. ```javascript [ { "_id": , "a": 1 }, { "_id": , "a": 3 } ] ``` -------------------------------- ### Compile Java Project with Gradle Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/README.md Command to compile the Java project using Gradle. This is a prerequisite for building the library. ```bash ./gradlew compileJava ``` -------------------------------- ### Update Result Example Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/src/test/resources/collection/replaceOneUpsert.expected.txt This shows the result of an update operation that did not match any documents. ```javascript AcknowledgedUpdateResult{matchedCount=0, modifiedCount=0, upsertedId=null} ``` -------------------------------- ### Run Tests with Gradle Source: https://github.com/mongodb-js/mongosh/blob/main/packages/java-shell/README.md Command to execute the project's tests using Gradle. Ensure a MongoDB instance is running and the `URI` variable in `src/test/resources/URI.txt` is correctly set. ```bash ./gradlew test ``` -------------------------------- ### IframeRuntime Usage Source: https://github.com/mongodb-js/mongosh/blob/main/packages/browser-repl/README.md Example of how to instantiate and use the IframeRuntime for code execution within an iframe sandbox. ```APIDOC ## `IframeRuntime` ### Description The `IframeRuntime` uses an iframe as a sandbox for code execution. Note that this sandbox is not fully isolated, as the top window is still accessible. ### Usage Example ```javascript import { IframeRuntime } from 'mongosh-browser-repl'; // Assuming 'serviceProvider' is an initialized service provider instance const runtime = new IframeRuntime(serviceProvider); ``` ### Parameters - **serviceProvider** - Required - An instance of the service provider. ```