<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Massimiliano (Max) Marcon</title>
    <description>The latest articles on DEV Community by Massimiliano (Max) Marcon (@mmarcon).</description>
    <link>https://dev.to/mmarcon</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F823595%2F6c8c2d1b-2a57-4535-92b7-e8f0676e554e.jpg</url>
      <title>DEV Community: Massimiliano (Max) Marcon</title>
      <link>https://dev.to/mmarcon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mmarcon"/>
    <language>en</language>
    <item>
      <title>MongoDB Shell tips and tricks</title>
      <dc:creator>Massimiliano (Max) Marcon</dc:creator>
      <pubDate>Mon, 21 Mar 2022 15:04:13 +0000</pubDate>
      <link>https://dev.to/mongodb/mongodb-shell-tips-and-tricks-1ceg</link>
      <guid>https://dev.to/mongodb/mongodb-shell-tips-and-tricks-1ceg</guid>
      <description>&lt;p&gt;&lt;code&gt;mongosh&lt;/code&gt; is the new &lt;a href="https://www.mongodb.com/products/shell" rel="noopener noreferrer"&gt;MongoDB Shell&lt;/a&gt;. Its advanced capabilities make it a great tool to work with MongoDB. In this blog, we will go through a few tips and tricks to help you get the most out of &lt;code&gt;mongosh&lt;/code&gt; and to make it a productivity booster for your MongoDB experience.&lt;/p&gt;

&lt;p&gt;Before we start with tips and tricks, it’s worth mentioning that &lt;code&gt;mongosh&lt;/code&gt; is built on top of Node.js, which means that the entire Node.js API is available inside &lt;code&gt;mongosh&lt;/code&gt;. This is a big step forward from the legacy &lt;code&gt;mongo&lt;/code&gt; shell, where the API available to developers was a limited JavaScript subset.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.mongodb.com/mongodb-shell/install/" rel="noopener noreferrer"&gt;Install &lt;code&gt;mongosh&lt;/code&gt; on your computer&lt;/a&gt; to follow along as you read this blog. If you’d prefer a pre-configured sandbox to experiment with, you can try &lt;a href="https://github.com/mmarcon/mongosh-docker" rel="noopener noreferrer"&gt;the Docker image&lt;/a&gt; I put together while writing the blog.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure &lt;code&gt;mongosh&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;As you’d expect from a modern tool for developers, you can customize &lt;code&gt;mongosh&lt;/code&gt; to suit your needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;config&lt;/code&gt; API
&lt;/h3&gt;

&lt;p&gt;The simplest way to configure &lt;code&gt;mongosh&lt;/code&gt; is to use the &lt;a href="https://docs.mongodb.com/mongodb-shell/reference/configure-shell-settings/" rel="noopener noreferrer"&gt;&lt;code&gt;config&lt;/code&gt; API&lt;/a&gt;. This API exposes a config object that lets you tweak the shell UX according to your preferences. You can find all the configuration options listed in the documentation. These are the ones our users and customers use more the most:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Adjust how deeply JSON objects returned by queries and &lt;/span&gt;
&lt;span class="c1"&gt;// MongoDB commands are expanded.&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;inspectDepth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// mongosh tries to print only the useful bits of&lt;/span&gt;
&lt;span class="c1"&gt;// MongoDB’s error messages.&lt;/span&gt;
&lt;span class="c1"&gt;// But sometimes, you may need to understand a bit better what&lt;/span&gt;
&lt;span class="c1"&gt;// is going on, or want to dig into how things work.&lt;/span&gt;
&lt;span class="c1"&gt;// With this config option, you can enable stack traces&lt;/span&gt;
&lt;span class="c1"&gt;// for all errors.&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;showStackTraces&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Configure the number of items displayed per cursor&lt;/span&gt;
&lt;span class="c1"&gt;// iteration, e.g., as a result of a query.&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;displayBatchSize&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Configure the external editor to be used by mongosh.&lt;/span&gt;
&lt;span class="c1"&gt;// Read more here: https://docs.mongodb.com/mongodb-shell/reference/editor-mode/&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;editor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;command&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you need to set default configuration options that apply to all &lt;code&gt;mongosh&lt;/code&gt; users on your operating system, you can do that with a &lt;a href="https://docs.mongodb.com/mongodb-shell/reference/configure-shell-settings-global/" rel="noopener noreferrer"&gt;global configuration file&lt;/a&gt; starting with &lt;code&gt;mongosh&lt;/code&gt; 1.22. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;.mongoshrc.js&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;For more advanced configuration, use JavaScript to add custom functions and shell helpers to your &lt;code&gt;.mongoshrc.js&lt;/code&gt; configuration file.&lt;/p&gt;

&lt;p&gt;For example, in my &lt;code&gt;.mongoshrc.js&lt;/code&gt;, I load JS files that contain query/aggregation helpers I use for my daily work analyzing data stored in MongoDB.&lt;/p&gt;

&lt;p&gt;One helpful function I use very often is &lt;code&gt;giveMeADate()&lt;/code&gt;, which makes it simpler to generate date objects to pass into my queries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;giveMeADate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;month&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;month&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;month&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;`0&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;`0&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ISODate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;year&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;day&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;T00:00:00.000Z`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can create libraries of helper functions structured in the way that makes the most sense to you and then &lt;a href="https://docs.mongodb.com/mongodb-shell/write-scripts/require-load-differences/" rel="noopener noreferrer"&gt;&lt;code&gt;load()&lt;/code&gt; or &lt;code&gt;require()&lt;/code&gt; them&lt;/a&gt; in your &lt;code&gt;.mongoshrc.js&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Customize your prompt
&lt;/h2&gt;

&lt;p&gt;I like colorful and informational prompts, which is why I am a happy &lt;a href="https://github.com/ohmyzsh/ohmyzsh" rel="noopener noreferrer"&gt;oh-my-zsh&lt;/a&gt; user. When I use &lt;code&gt;mongosh&lt;/code&gt; to work with my data, I want a quick overview of my connection status and information about the cluster I am connected to.&lt;/p&gt;

&lt;p&gt;That’s why my prompt looks like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa8iqk2c2la0i4boeu0hc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa8iqk2c2la0i4boeu0hc.png" alt="mongosh informational prompt"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It says that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Green cloud: I am connected to &lt;a href="https://www.mongodb.com/cloud/atlas/signup?utm_campaign=explainer&amp;amp;utm_source=cta_link_50&amp;amp;utm_medium=asset&amp;amp;utm_term=consideration" rel="noopener noreferrer"&gt;MongoDB Atlas&lt;/a&gt;, our cloud database.&lt;/li&gt;
&lt;li&gt;Little &lt;code&gt;1&lt;/code&gt;: The cluster has a primary so it’s all good and healthy.&lt;/li&gt;
&lt;li&gt;My &lt;a href="https://docs.mongodb.com/manual/core/read-preference/" rel="noopener noreferrer"&gt;read preference&lt;/a&gt; is secondary.&lt;/li&gt;
&lt;li&gt;Chart icon: My queries will run against &lt;a href="https://docs.atlas.mongodb.com/reference/replica-set-tags/#node-types" rel="noopener noreferrer"&gt;an analytics node&lt;/a&gt; and therefore won’t affect the normal cluster operations. If you want to learn more about this, check out the &lt;a href="https://docs.mongodb.com/manual/core/workload-isolation/" rel="noopener noreferrer"&gt;MongoDB documentation about workload isolation&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;My database is “test.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;How do you set up your prompt to look like this? Easy: Just &lt;a href="https://docs.mongodb.com/mongodb-shell/mongoshrc/#examples" rel="noopener noreferrer"&gt;include a global function named “prompt”&lt;/a&gt; in your &lt;code&gt;.mongoshrc.js&lt;/code&gt; file and make it return the information you need in the format you prefer. The prompt in the screenshot above is generated with &lt;a href="https://gist.github.com/mmarcon/ee6440fc34df05923e76b9b3587b593a/5016f88f1503f8a8e925781af283bb42c0d59668" rel="noopener noreferrer"&gt;this code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And if you like pretty icons as much as I do, configure your terminal application to use &lt;a href="https://github.com/ryanoasis/nerd-fonts" rel="noopener noreferrer"&gt;Nerd Fonts&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automatically time out sessions
&lt;/h2&gt;

&lt;p&gt;When I connect to my production clusters, I often worry about leaving a shell session open accidentally. For security reasons, or due to the security policies and requirements of your organization, you may want your shell sessions to time out when they are not used for some time.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;mongosh&lt;/code&gt;, that is quite easy to do. You can add this behavior just with a few lines of JavaScript in your &lt;code&gt;.mongoshrc.js&lt;/code&gt;. A few lines of code can automatically call &lt;code&gt;exit()&lt;/code&gt; after a certain amount of inactivity or reset a countdown timer whenever a prompt is generated.&lt;/p&gt;

&lt;p&gt;In my prompt customization script, you can see &lt;a href="https://gist.github.com/mmarcon/ee6440fc34df05923e76b9b3587b593a#file-prompt-js-L12" rel="noopener noreferrer"&gt;how that is done&lt;/a&gt;. The prompt even includes a nice visual hint (the stopwatch on the right) to show that the session will time out.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxrb194ghknxjy3qd74n5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxrb194ghknxjy3qd74n5.png" alt="mongosh prompt with session timeout"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this simple approach, a shell session could end even if a query is running. Therefore, I included &lt;a href="https://gist.github.com/mmarcon/ee6440fc34df05923e76b9b3587b593a#file-prompt-js-L63" rel="noopener noreferrer"&gt;a &lt;code&gt;stayLoggedIn()&lt;/code&gt; helper function&lt;/a&gt; that disables the timeout for the current session.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write query results to a file
&lt;/h2&gt;

&lt;p&gt;It’s common for users to need to save the result of a query or a diagnostic command in a file. In &lt;code&gt;mongosh&lt;/code&gt;, that is really easy to do with the standard Node.js API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;sample_mflix&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;thrillers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;movies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;genres&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Thriller&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;sample_mflix&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;thrillers.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;EJSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;thrillers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toArray&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above queries the "movies" collection to find all the thrillers and then writes them out as &lt;a href="https://docs.mongodb.com/manual/reference/mongodb-extended-json/" rel="noopener noreferrer"&gt;Extended JSON&lt;/a&gt; into a file named &lt;code&gt;thrillers.json&lt;/code&gt; in the current working directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fetch data from an API and store it in MongoDB
&lt;/h2&gt;

&lt;p&gt;Sometimes, you’ll need to fetch data from an HTTP endpoint in JSON and store it in MongoDB. Before &lt;code&gt;mongosh&lt;/code&gt;, you had to script this operation with your favorite programming language. Now, you can do it all inside the MongoDB Shell. Because the entire Node.js API is available to you, including all the HTTP functionality, you can write a script that fetches the data and then writes it into MongoDB with an &lt;code&gt;insertMany()&lt;/code&gt; or with the &lt;code&gt;Bulk API&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It’s even easier to do this if you rely on one of the NPM packages that were built to make HTTP more convenient. In the example below, I am using &lt;a href="https://www.npmjs.com/package/node-fetch" rel="noopener noreferrer"&gt;node-fetch&lt;/a&gt;. To follow this example, you will need to have Node.js and npm installed on your computer.&lt;/p&gt;

&lt;p&gt;First of all, create a &lt;code&gt;fetch-and-store.js&lt;/code&gt; file with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;node-fetch&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;opencollective&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getOpenCollectiveData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://opencollective.com/webpack/members/organizations.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;members&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;members&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;members&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;getOpenCollectiveData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This simple script fetches all the Open Collective members and their information and stores them into a collection named “members” in the “opencollective” database.&lt;br&gt;
To run the script, we need to make sure node-fetch is installed. The command below installs node-fetch in the current directory, which is enough for &lt;code&gt;mongosh&lt;/code&gt; to find the package. You can also install it globally to have it always available no matter what your working directory is.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;node-fetch@2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can run the script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;mongosh &amp;lt;connection string&amp;gt; fetch-and-store.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the script has finished, the data pulled from the Open Collective endpoint will be stored in MongoDB. You can see that from the &lt;a href="https://www.mongodb.com/products/compass" rel="noopener noreferrer"&gt;Compass&lt;/a&gt; screenshot below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbm0myitezc03sspxcere.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbm0myitezc03sspxcere.png" alt="Open Collective data in MongoDB Compass"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Generate large volumes of synthetic data
&lt;/h2&gt;

&lt;p&gt;Sometimes, you just want to generate large volumes of synthetic data for development and testing. One great module that helps with this is &lt;a href="https://github.com/ngneat/falso" rel="noopener noreferrer"&gt;Falso&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Similarly to what we did with node-fetch above, we’ll install it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @ngneat/falso
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then we’ll create &lt;code&gt;fake-people.js&lt;/code&gt; with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;falso&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@ngneat/falso&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;people&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;falso&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randFirstName&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;falso&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randLastName&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="na"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;falso&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randAvatar&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="na"&gt;phoneNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;falso&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randPhoneNumber&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;falso&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randEmail&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fake-data&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;people&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the script has run, there will be a “people” collection in a “fake-data” database with 1000 fake people’s data inside.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analyze the schema of your collection
&lt;/h2&gt;

&lt;p&gt;As the amount of data that you store in MongoDB grows and you have multiple applications reading and writing into your clusters, it might be useful to take a look at your collection and check that the schema is what you expect and is as homogeneous as possible across your documents.&lt;/p&gt;

&lt;p&gt;This is something you could probably write a script for. The good news is that we have already scripted it for you, in the form of a &lt;a href="https://docs.mongodb.com/mongodb-shell/snippets/" rel="noopener noreferrer"&gt;shell snippet&lt;/a&gt;. Snippets are scripts that are packaged and stored in a registry to facilitate sharing and reuse. We have &lt;a href="https://github.com/mongodb-labs/mongosh-snippets/tree/main/snippets/analyze-schema" rel="noopener noreferrer"&gt;one dedicated to schema analysis&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Using it is extremely simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;sample_mflix&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;snippet&lt;/span&gt; &lt;span class="nx"&gt;install&lt;/span&gt; &lt;span class="nx"&gt;analyze&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;schema&lt;/span&gt;
&lt;span class="nx"&gt;sample_mflix&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;schema&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below you can see an example of what that looks like and how you can use it to spot outliers in your data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1s7fnfhszmoywtegznd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa1s7fnfhszmoywtegznd.png" alt="Schema analysis snippet in mongosh"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this list of tips and tricks helped you discover how to get the most out of the MongoDB Shell. Do you have other &lt;code&gt;mongosh&lt;/code&gt; customizations, scripts, and tweaks that you’d like to share with us and the community? Let us know in our &lt;a href="https://www.mongodb.com/community/forums/tag/mongodb-shell" rel="noopener noreferrer"&gt;community forum&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And if you have suggestions for how to improve the experience with &lt;code&gt;mongosh&lt;/code&gt;, feel free to &lt;a href="https://feedback.mongodb.com/forums/929233-mongodb-shell" rel="noopener noreferrer"&gt;submit ideas in our feedback portal&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>database</category>
      <category>productivity</category>
      <category>tooling</category>
    </item>
  </channel>
</rss>
