<?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: Donald Feury</title>
    <description>The latest articles on DEV Community by Donald Feury (@dak425).</description>
    <link>https://dev.to/dak425</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%2F166374%2F5a66fd4e-7a9b-4d73-a609-9e8546ffbc47.jpg</url>
      <title>DEV Community: Donald Feury</title>
      <link>https://dev.to/dak425</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dak425"/>
    <language>en</language>
    <item>
      <title>How to create the content hash for a Dropbox file upload in Elixir</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Sat, 05 Aug 2023 22:43:01 +0000</pubDate>
      <link>https://dev.to/dak425/how-to-create-the-content-hash-for-a-dropbox-file-upload-in-elixir-2cp0</link>
      <guid>https://dev.to/dak425/how-to-create-the-content-hash-for-a-dropbox-file-upload-in-elixir-2cp0</guid>
      <description>&lt;p&gt;Dropbox has a specific way they want you to calculate the content hash for a file you are uploading. You can find the instructions &lt;a href="https://www.dropbox.com/developers/reference/content-hash"&gt;here&lt;/a&gt; but to summarize:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Split the file into blocks of 4 MB (4,194,304 or 4 * 1024 * 1024 bytes). The last block (if any) may be smaller than 4 MB.&lt;/li&gt;
&lt;li&gt;Compute the hash of each block using SHA-256.&lt;/li&gt;
&lt;li&gt;Concatenate the hash of all blocks in the binary format to form a single binary string.&lt;/li&gt;
&lt;li&gt;Compute the hash of the concatenated string using SHA-256. Output the resulting hash in hexadecimal format.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can convert each of these steps into parts of a pipeline!&lt;/p&gt;

&lt;p&gt;First we need to start reading in a file, split into chunks of the specified size. &lt;a href="https://hexdocs.pm/elixir/File.html#stream!/3"&gt;File.stream!&lt;/a&gt; is a good way of achieving this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chunk_size = 4 * 1024 * 1024

File.stream!("my_file.txt", [], chunk_size)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we need to create a new hash from each chunk. Iterating over the stream chunks with &lt;a href="https://hexdocs.pm/elixir/Enum.html#map/2"&gt;Enum.map&lt;/a&gt; and passing those chunks into the Erlang module &lt;a href="https://hexdocs.pm/elixir/Enum.html#map/2"&gt;:crypto.hash&lt;/a&gt; will get us our chunk hashes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chunk_size = 4 * 1024 * 1024

File.stream!("my_file.txt", [], chunk_size)
|&amp;gt; Enum.map(&amp;amp;:crypto.hash(:sha256, &amp;amp;1))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, we'll combine all individual hashes into one big chunky string of hashes. &lt;a href="https://hexdocs.pm/elixir/Enum.html#join/2"&gt;Enum.join&lt;/a&gt; will let us that list of strings and slap them together&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chunk_size = 4 * 1024 * 1024

File.stream!("my_file.txt", [], chunk_size)
|&amp;gt; Enum.map(&amp;amp;:crypto.hash(:sha256, &amp;amp;1))
|&amp;gt; Enum.join()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Getting warmer, time to take that big boy and get one final hash using &lt;a href="https://hexdocs.pm/elixir/Enum.html#map/2"&gt;:crypto.hash&lt;/a&gt; again. We'll have to wrap it inside an anonymous function since we need to pass the string in as the second argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chunk_size = 4 * 1024 * 1024

File.stream!("my_file.txt", [], chunk_size)
|&amp;gt; Enum.map(&amp;amp;:crypto.hash(:sha256, &amp;amp;1))
|&amp;gt; Enum.join()
|&amp;gt; (&amp;amp;:crypto.hash(:sha256, &amp;amp;1)).()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Home stretch! Lastily, base 16 encode the final hash using &lt;a href="https://hexdocs.pm/elixir/Base.html#encode16/2"&gt;Base.encode16&lt;/a&gt;. Don't forget to lowercase it by passing in &lt;code&gt;case: :lower&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chunk_size = 4 * 1024 * 1024

File.stream!("my_file.txt", [], chunk_size)
|&amp;gt; Enum.map(&amp;amp;:crypto.hash(:sha256, &amp;amp;1))
|&amp;gt; Enum.join()
|&amp;gt; (&amp;amp;:crypto.hash(:sha256, &amp;amp;1)).()
|&amp;gt; Base.encode16(case: :lower)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There ya go! You should now be able to create the content hash for a file that you intend to upload to Dropbox.&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>dropbox</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to perform a JOIN in MongoDB</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Mon, 31 Jan 2022 00:45:32 +0000</pubDate>
      <link>https://dev.to/dak425/how-to-perform-a-join-in-mongodb-457i</link>
      <guid>https://dev.to/dak425/how-to-perform-a-join-in-mongodb-457i</guid>
      <description>&lt;p&gt;For a full overview of MongoDB and all my posts on it, check out my &lt;a href="https://donaldfeury.xyz/introduction-to-mongodb/"&gt;overview&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have relational data in your MongoDB instance, you can perform an operation similar to a JOIN typically done in SQL queries.&lt;/p&gt;

&lt;p&gt;Consider the following data set:&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;podcasts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;insertMany&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Off The Clock&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Technology&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;rss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://anchor.fm/s/76aafa5c/podcast/rss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;id&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;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Tech Over Tea&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Technology&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;rss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://anchor.fm/s/149fd51c/podcast/rss&lt;/span&gt;&lt;span class="dl"&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;episodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;insertMany&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;podcast_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Resume Tips&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;published_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2022-01-11&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;podcast_id&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;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#75 Welcome Our Hacker Neko Waifu | Cyan Nyan&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;published_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2021-08-04&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;podcast_id&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;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Square Enix Refuses To Take My Money | Solo&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;published_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2022-01-26&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;podcast_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Find the Right Job&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;published_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2022-01-25&lt;/span&gt;&lt;span class="dl"&gt;"&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;If you want to get every podcast with all of its associated episodes, you can accomplish this using the &lt;code&gt;$lookup&lt;/code&gt; aggregation stage.&lt;/p&gt;

&lt;p&gt;The aggregation would look something like this:&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;podcasts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$lookup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;episodes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Name of the other collection to "join" from&lt;/span&gt;
        &lt;span class="na"&gt;localField&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Name of the field your current documents contain to compare with&lt;/span&gt;
        &lt;span class="na"&gt;foreignField&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;podcast_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Name of field to compare to in the "from" collection's documents&lt;/span&gt;
        &lt;span class="na"&gt;as&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;episodes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="c1"&gt;// What to call the field that contains the array of sub documents that matched&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;Any documents in the &lt;code&gt;episodes&lt;/code&gt; collection that had a &lt;code&gt;podcast_id&lt;/code&gt; that matched one of the &lt;code&gt;podcasts&lt;/code&gt; documents would have been added to an array of sub-documents called &lt;code&gt;episodes&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The result would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;id:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;name:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Off The Clock"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;category:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Technology"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;rss:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://anchor.fm/s/76aafa5c/podcast/rss"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;episodes:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;podcast_id:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;title:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Resume Tips"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;published_on:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2022-01-11"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;podcast_id:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;title:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Find the Right Job"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;published_on:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2022-01-25"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;id:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;name:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Tech Over Tea"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;category:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Technology"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;rss:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://anchor.fm/s/149fd51c/podcast/rss"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="err"&gt;episodes:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;podcast_id:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;title:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"#75 Welcome Our Hacker Neko Waifu | Cyan Nyan"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;published_on:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2021-08-04"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;podcast_id:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;title:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Square Enix Refuses To Take My Money | Solo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;published_on:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2022-01-26"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would be the equivalent of the SQL query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;
    &lt;span class="n"&gt;podcasts&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;episodes&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;episodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;podcast_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;podcasts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I specified &lt;code&gt;LEFT JOIN&lt;/code&gt; in the SQL example because if no documents match the &lt;code&gt;$lookup&lt;/code&gt; aggregation stage, the podcasts would still be returned with the &lt;code&gt;episodes&lt;/code&gt; field being an empty array.&lt;/p&gt;




&lt;p&gt;Did you find this information useful? If so, consider heading over to my &lt;a href="https://donaldfeury.xyz/donate/"&gt;donation&lt;/a&gt; page and drop me some support.&lt;/p&gt;

&lt;p&gt;Want to ask a question or just chat? Contact me &lt;a href="https://donaldfeury.xyz/contact"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
    </item>
    <item>
      <title>When to use MongoDB aggregations instead of queries</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Mon, 24 Jan 2022 01:54:50 +0000</pubDate>
      <link>https://dev.to/dak425/when-to-use-mongodb-aggregations-instead-of-queries-2p37</link>
      <guid>https://dev.to/dak425/when-to-use-mongodb-aggregations-instead-of-queries-2p37</guid>
      <description>&lt;p&gt;When it comes to reading data out of your MongoDB instance you have two options, a query using the standard query language or an aggregation pipeline. While you can often use either for many use cases, there are differences in how they are used and what use cases they are a good fit for.&lt;/p&gt;

&lt;p&gt;For use with the following examples you can insert the following documents into your MongoDB instance:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;If you haven't inserted multiple documents into MongoDB before, you can &lt;a href="https://donaldfeury.xyz/how-to-bulk-insert-documents-into-a-mongodb-collection/"&gt;read about that here&lt;/a&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;podcasts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;insertMany&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Podcast 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Business&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;rss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://mypodcast1.com/podcast/rss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;id&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;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Podcast 2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Business&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;rss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://podcast2.net/rss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Podcast 3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Health &amp;amp; Wellness&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;rss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://podcast3.xyz/feed/rss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Podcast 4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Technology&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;rss&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://podcast4.com/rss&lt;/span&gt;&lt;span class="dl"&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;episodes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;insertMany&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;podcast_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Anabolic Recipes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;published_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2020-01-02&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;podcast_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Getting Going with Go&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;published_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2021-02-24&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;podcast_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Talk About Money Early&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;published_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2022-01-01&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;podcast_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Charge More by Saying No&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;publshed_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2021-12-01&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;podcast_id&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;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Candidate Interview Strategies&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;published_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2020-05-01&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;podcast_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;The Magic of Pattern Matching&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;published_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2022-01-01&lt;/span&gt;&lt;span class="dl"&gt;"&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;h2&gt;
  
  
  Ease of use
&lt;/h2&gt;

&lt;p&gt;MongoDB's query language and its related methods are meant to be straightforward to use. Compared to aggregations they are easier to read and are less prone to error.&lt;/p&gt;

&lt;p&gt;Take the example of wanting to filter out and return only certain documents. Using the query language is as simple as passing in an object describing how to filter the objects to the &lt;code&gt;find&lt;/code&gt; method.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;You can read more about &lt;a href="https://donaldfeury.xyz/how-to-retrieve-specific-documents-from-mongodb-using-filters/"&gt;using the find method here&lt;/a&gt;.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;podcasts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;find&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Business&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To do the same thing with an aggregation would require the use of &lt;code&gt;$match&lt;/code&gt; aggregation stage.&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;podcasts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$match&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;category&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Business&lt;/span&gt;&lt;span class="dl"&gt;"&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;h2&gt;
  
  
  Use Case
&lt;/h2&gt;

&lt;p&gt;While the strength of the standard query language is its simplicity, that also limits what problems it can solve. Aggregations are essentially the opposite, harder to use but much more powerful, allowing them to accomplish certain use cases that are unable to be done by simple queries.&lt;/p&gt;

&lt;p&gt;The standard query language is a good solution for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Returning/affecting only certain documents that match provided filter&lt;/li&gt;
&lt;li&gt;When reading data back out, only including certain fields&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Anything that falls outside the scope of those scenarios is either &lt;em&gt;better&lt;/em&gt; done or required to be done by aggregations.&lt;/p&gt;

&lt;p&gt;One such example and the earliest one I personally needed to use aggregations was when wanting to perform an operation similar to a join in a SQL query.&lt;/p&gt;

&lt;p&gt;This simply cannot be done using the available query methods and language but CAN be accomplished using the &lt;code&gt;$lookup&lt;/code&gt; aggregation stage.&lt;/p&gt;

&lt;p&gt;What if we wanted to get all of our podcasts including each episode associated with that podcast:&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;podcasts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;aggregate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;$lookup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;episodes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;localField&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;foreignField&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;podcast_id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;as&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;episodes&lt;/span&gt;&lt;span class="dl"&gt;"&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;



</description>
      <category>mongodb</category>
    </item>
    <item>
      <title>How to do conditional execution in Bash</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Sat, 23 Oct 2021 23:15:14 +0000</pubDate>
      <link>https://dev.to/dak425/how-to-do-conditional-execution-in-bash-300j</link>
      <guid>https://dev.to/dak425/how-to-do-conditional-execution-in-bash-300j</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2Yx2gh-E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.unsplash.com/photo-1607877361964-bf792b65e593%3Fcrop%3Dentropy%26cs%3Dtinysrgb%26fit%3Dmax%26fm%3Djpg%26ixid%3DMnwxMTc3M3wwfDF8c2VhcmNofDF8fHpzaHxlbnwwfHx8fDE2MzUwMzA3ODQ%26ixlib%3Drb-1.2.1%26q%3D80%26w%3D2000" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2Yx2gh-E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.unsplash.com/photo-1607877361964-bf792b65e593%3Fcrop%3Dentropy%26cs%3Dtinysrgb%26fit%3Dmax%26fm%3Djpg%26ixid%3DMnwxMTc3M3wwfDF8c2VhcmNofDF8fHpzaHxlbnwwfHx8fDE2MzUwMzA3ODQ%26ixlib%3Drb-1.2.1%26q%3D80%26w%3D2000" alt="How to do conditional execution in Bash"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Bash scripts, any command following a &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; will only execute if the previous conditional or command succeeded (returned 0)&lt;/p&gt;

&lt;p&gt;The following script will only print out the variable if the conditional succeeds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[[-z "$1"]] &amp;amp;&amp;amp; echo "$1"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, any command following a &lt;code&gt;||&lt;/code&gt; will only execute if the previous conditional or command failed (returned a non zero)&lt;/p&gt;

&lt;p&gt;The following script will only print out the variable if the conditional fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[[-z "$1"]] || echo "$1"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Related
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://donaldfeury.xyz/tag/bash"&gt;Posts about Bash&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Did you find this information useful? If so, consider heading over to my &lt;a href="https://donaldfeury.xyz/donate/"&gt;donation&lt;/a&gt; page and drop me some support.&lt;/p&gt;

&lt;p&gt;Want to ask a question or just chat? Contact me &lt;a href="https://donaldfeury.xyz/contact"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>bash</category>
      <category>linux</category>
    </item>
    <item>
      <title>How to check if a string is empty in Bash</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Fri, 22 Oct 2021 16:36:15 +0000</pubDate>
      <link>https://dev.to/dak425/how-to-check-if-a-string-is-empty-in-bash-264l</link>
      <guid>https://dev.to/dak425/how-to-check-if-a-string-is-empty-in-bash-264l</guid>
      <description>&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%2Fimages.unsplash.com%2Fphoto-1629654291663-b91ad427698f%3Fcrop%3Dentropy%26cs%3Dtinysrgb%26fit%3Dmax%26fm%3Djpg%26ixid%3DMnwxMTc3M3wwfDF8c2VhcmNofDJ8fEJhc2h8ZW58MHx8fHwxNjM0OTIwNDU4%26ixlib%3Drb-1.2.1%26q%3D80%26w%3D2000" 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%2Fimages.unsplash.com%2Fphoto-1629654291663-b91ad427698f%3Fcrop%3Dentropy%26cs%3Dtinysrgb%26fit%3Dmax%26fm%3Djpg%26ixid%3DMnwxMTc3M3wwfDF8c2VhcmNofDJ8fEJhc2h8ZW58MHx8fHwxNjM0OTIwNDU4%26ixlib%3Drb-1.2.1%26q%3D80%26w%3D2000" alt="How to check if a string is empty in Bash" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To check if a string is empty in a Bash script, use the &lt;code&gt;-z&lt;/code&gt; conditional which returns 0 (true) if the length of the string is 0 and 1 (false) if it is greater than 0.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

myVar="hello"

if [[-z "$myVar"]]; then
    echo "myVar is empty"
else
    echo "myVar is not empty"
fi



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Alternatively, you can check if a bash string is NOT empty using &lt;code&gt;-n&lt;/code&gt; and take the opposite using &lt;code&gt;!&lt;/code&gt;.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;myVar=""&lt;/p&gt;

&lt;p&gt;if [[! -n "$myVar"]]; then&lt;br&gt;
    echo "myVar is empty"&lt;br&gt;
else&lt;br&gt;
    echo "myVar is not empty"&lt;br&gt;
fi&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Related&lt;br&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://donaldfeury.xyz/tag/bash" rel="noopener noreferrer"&gt;All my posts on Bash&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Did you find this information useful? If so, consider heading over to my &lt;a href="https://donaldfeury.xyz/donate/" rel="noopener noreferrer"&gt;donation&lt;/a&gt; page and drop me some support.&lt;/p&gt;

&lt;p&gt;Want to ask a question or just chat? Contact me &lt;a href="https://donaldfeury.xyz/contact" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>bash</category>
      <category>linux</category>
    </item>
    <item>
      <title>Use the filter positional operator in MongoDB to update specific array elements</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Wed, 20 Oct 2021 16:00:00 +0000</pubDate>
      <link>https://dev.to/dak425/use-the-filter-positional-operator-in-mongodb-to-update-specific-array-elements-2d2</link>
      <guid>https://dev.to/dak425/use-the-filter-positional-operator-in-mongodb-to-update-specific-array-elements-2d2</guid>
      <description>&lt;p&gt;For a full overview of MongoDB and all my posts on it, check out my &lt;a href="https://donaldfeury.xyz/introduction-to-mongodb/"&gt;overview&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;MongoDB provides a filter positional operator &lt;code&gt;$[&amp;lt;name_of_filter&amp;gt;]&lt;/code&gt; to only update some elements in an array. If you want to update all the elements in the array, &lt;a href="https://donaldfeury.xyz/use-the-all-positional-operator-in-mongodb-to-update-all-elements-in-an-array/"&gt;use the all positional operator in MongoDB to update all elements in an array&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You must define the filters to use in a property called &lt;code&gt;arrayFilters&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With the following data inserted into a collection called &lt;code&gt;games&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.games.insertMany(
    {
        name: "Genshin Impact",
        reviewScores: [8, 6, 9, 5]
    },
    {
        name: "Factorio",
        reviewScores: [7, 7, 10, 8]
    },
    {
        name: "Bloodborne",
        reviewScores: [9, 8, 9, 9]
    }
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the game &lt;code&gt;Genshin Impact&lt;/code&gt;, increase all the reviews by 1 that are less than 9.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.games.updateMany(
    { name: "Genshin Impact" },
    { $inc: { "reviewScores.$[reviews]": 1 } },
    { arrayFilters: [
        { "reviews": { $lt: 9 } }
    ]}
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice in the &lt;code&gt;$inc&lt;/code&gt; &lt;a href="https://donaldfeury.xyz/how-to-update-a-single-document-into-a-mongodb-collection-2/"&gt;update operator&lt;/a&gt;, we reference the array filter defined called &lt;code&gt;reviews&lt;/code&gt; to tell MongoDB which elements to update.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://donaldfeury.xyz/get-all-the-documents-out-of-a-collection-in-mongodb/"&gt;Use the find method to read data back out of MongoDB&lt;/a&gt; to view the results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    name: "Genshin Impact",
    reviewScores: [9, 7, 9, 6]
},
{
    name: "Factorio",
    reviewScores: [7, 7, 10, 8]
},
{
    name: "Bloodborne",
    reviewScores: [9, 8, 9, 9]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Related
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://donaldfeury.xyz/tag/mongodb"&gt;My other posts on MongoDB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://donaldfeury.xyz/use-the-all-positional-operator-in-mongodb-to-update-all-elements-in-an-array/"&gt;All Positional Array Operator&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Did you find this information useful? If so, consider heading over to my &lt;a href="https://donaldfeury.xyz/donate/"&gt;donation&lt;/a&gt; page and drop me some support.&lt;/p&gt;

&lt;p&gt;Want to ask a question or just chat? Contact me &lt;a href="https://donaldfeury.xyz/contact"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>database</category>
    </item>
    <item>
      <title>Use the all positional operator in MongoDB to update all elements in an array</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Tue, 19 Oct 2021 13:22:00 +0000</pubDate>
      <link>https://dev.to/dak425/use-the-all-positional-operator-in-mongodb-to-update-all-elements-in-an-array-3p3g</link>
      <guid>https://dev.to/dak425/use-the-all-positional-operator-in-mongodb-to-update-all-elements-in-an-array-3p3g</guid>
      <description>&lt;p&gt;For a full overview of MongoDB and all my posts on it, check out my &lt;a href="https://donaldfeury.xyz/introduction-to-mongodb/"&gt;overview&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;MongoDB provides a special update operator &lt;code&gt;$[]&lt;/code&gt; to apply an update operation to all elements in an array belonging to matching documents.&lt;/p&gt;

&lt;p&gt;With the following data inserted into a collection called &lt;code&gt;games&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.games.insertMany(
    {
        name: "Genshin Impact",
        reviewScores: [8, 6, 9, 5]
    },
    {
        name: "Factorio",
        reviewScores: [7, 7, 10, 8]
    },
    {
        name: "Bloodborne",
        reviewScores: [9, 8, 9, 9]
    }
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For every game that does NOT have a score of 10 in its reviews, increase all the review scores by 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.games.updateMany(
    { reviewScores: { $ne: 10 } },
    { $inc: { "reviewScores.$[]": 1 } }
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://donaldfeury.xyz/get-all-the-documents-out-of-a-collection-in-mongodb/"&gt;Use the find method&lt;/a&gt; to read all the data back out to view the results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    name: "Genshin Impact",
    reviewScores: [9, 7, 10, 6]
},
{
    name: "Factorio",
    reviewScores: [7, 7, 10, 8]
},
{
    name: "Bloodborne",
    reviewScores: [10, 9, 10, 10]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Related
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://donaldfeury.xyz/tag/mongodb"&gt;All posts on MongoDB&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Did you find this information useful? If so, consider heading over to my &lt;a href="https://donaldfeury.xyz/donate/"&gt;donation&lt;/a&gt; page and drop me some support.&lt;/p&gt;

&lt;p&gt;Want to ask a question or just chat? Contact me &lt;a href="https://donaldfeury.xyz/contact"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>database</category>
    </item>
    <item>
      <title>This is how to do upserting in MongoDB</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Sun, 22 Aug 2021 18:25:14 +0000</pubDate>
      <link>https://dev.to/dak425/this-is-how-to-do-upserting-in-mongodb-2m69</link>
      <guid>https://dev.to/dak425/this-is-how-to-do-upserting-in-mongodb-2m69</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8Z0O-erm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://donaldfeury.xyz/content/images/2021/08/MongoDB_Logo2-4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8Z0O-erm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://donaldfeury.xyz/content/images/2021/08/MongoDB_Logo2-4.png" alt="This is how to do upserting in MongoDB"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For a full overview of MongoDB and all my posts on it, check out my &lt;a href="https://donaldfeury.xyz/introduction-to-mongodb/"&gt;overview&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Upserting is a database concept that combines inserting and updating that MongoDB supports. To perform an upsert during an update operation, you must pass an additional argument to the &lt;code&gt;update&lt;/code&gt;, &lt;code&gt;updateOne&lt;/code&gt;, or &lt;code&gt;updateMany&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;With the given data inserted in a collection called &lt;code&gt;users&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.users.insertMany([
    {
        _id: 1,
        name: "John Doe",
        email: "doe@doe.com",
        admin: true
    },
    {
        _id: 2,
        name: "Jane Doe",
        email: "jane@doe.com",
        admin: true
    },
    {
        _id: 3,
        name: "Billy Bob",
        email: "billy@bob.com",
        admin: false
    },
    {
        _id: 4
        name: "Steve Stevenson",
        email: "steve@test.com",
        admin: true
    },
])

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the following command is used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.users.updateOne({_id: 5}, {$set: {admin: true}})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It won't do anything as there is no document matching the query of &lt;code&gt;_id = 5&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If upserting is enabled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.users.updateOne(
    {_id: 5},
    { $set: { admin: true } },
    { upsert: true }
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since there is no document matching the query, a new document is inserting using the given &lt;code&gt;_id&lt;/code&gt; and the result of the update operators.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://donaldfeury.xyz/get-all-the-documents-out-of-a-collection-in-mongodb/"&gt;Use the find method to read data back out of MongoDB&lt;/a&gt; to check the collection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.users.find()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Will yield:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    _id: 1,
    name: "John Doe",
    email: "doe@doe.com",
    admin: true
},
{
    _id: 2,
    name: "Jane Doe",
    email: "jane@doe.com",
    admin: true
},
{
    _id: 3,
    name: "Billy Bob",
    email: "billy@bob.com",
    admin: false
},
{
    _id: 4
    name: "Steve Stevenson",
    email: "steve@test.com",
    admin: true
},
{
    _id: 5
    admin: true
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>mongodb</category>
    </item>
    <item>
      <title>How to replicate the output of the FullScreen template from StreamLadder</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Tue, 17 Aug 2021 23:37:46 +0000</pubDate>
      <link>https://dev.to/dak425/how-to-replicate-the-output-of-the-fullscreen-template-from-streamladder-684</link>
      <guid>https://dev.to/dak425/how-to-replicate-the-output-of-the-fullscreen-template-from-streamladder-684</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--27wf0b8f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://donaldfeury.xyz/content/images/2021/08/MMPEG_Logo2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--27wf0b8f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://donaldfeury.xyz/content/images/2021/08/MMPEG_Logo2.png" alt="How to replicate the output of the FullScreen template from StreamLadder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://streamladder.com"&gt;StreamLadder&lt;/a&gt; is a nifty little tool for creating vertical versions of your Twitch clips for platforms like Tiktok and YouTube Shorts. However, I like tinkering with &lt;a href="https://donaldfeury.xyz/tag/ffmpeg"&gt;FFmpeg&lt;/a&gt; so I replicated the output with a simple command.&lt;/p&gt;

&lt;p&gt;If we look at the Fullscreen template, all it's essentially doing is cropping the original clip down into a 9:16 ratio.&lt;/p&gt;

&lt;p&gt;To do the same with &lt;a href="https://donaldfeury.xyz/tag/ffmpeg"&gt;FFmpeg&lt;/a&gt; we can just do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env sh

# the source video (in this case, a twitch clip)
input=$1

# where you want to save the processed version
output=$2

ffmpeg \
    -i "$input" \
    -vf "crop=w=in_h*9/16:h=in_h,scale=1080x1920" \
    -vcodec libx264 \
    -crf 23 \
    -preset veryfast \
    -c:a copy \
    -s 1080x1920 \
    "$output"

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main part of this command doing all the work is the video filter, denoted with &lt;code&gt;-vf&lt;/code&gt;. We do &lt;code&gt;w=in_h*9/16&lt;/code&gt; to set the width of the video to the 9:16 ratio compared to what the height of the video is while we keep the height the same with &lt;code&gt;h=in_h&lt;/code&gt;. We finally scale the video up to 1080x1920 with the &lt;code&gt;scale&lt;/code&gt; filter.&lt;/p&gt;

&lt;p&gt;If the video quality looks a bit pixelated from scaling up too much, anywhere you see &lt;code&gt;1080x1920&lt;/code&gt; replace with a smaller equivalent like &lt;code&gt;720x1280&lt;/code&gt;&lt;/p&gt;

</description>
      <category>ffmpeg</category>
      <category>streamladder</category>
    </item>
    <item>
      <title>How to do bulk updates in MongoDB</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Mon, 16 Aug 2021 18:59:17 +0000</pubDate>
      <link>https://dev.to/dak425/how-to-do-bulk-updates-in-mongodb-n3f</link>
      <guid>https://dev.to/dak425/how-to-do-bulk-updates-in-mongodb-n3f</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WuSstgut--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://donaldfeury.xyz/content/images/2021/08/MongoDB_Logo2-3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WuSstgut--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://donaldfeury.xyz/content/images/2021/08/MongoDB_Logo2-3.png" alt="How to do bulk updates in MongoDB"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For a full overview of MongoDB and all my posts on it, check out my &lt;a href="https://donaldfeury.xyz/introduction-to-mongodb/"&gt;overview&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://donaldfeury.xyz/how-to-update-a-single-document-into-a-mongodb-collection-2/"&gt;MongoDB provides several ways to update specifically one document&lt;/a&gt; and bulk updating is done in almost the same way.&lt;/p&gt;

&lt;p&gt;Using the following data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.podcasts.insertMany([
    {
        "name": "Tech Over Tea",
        "episodeName": "#75 Welcome Our Hacker Neko Waifu | Cyan Nyan",
        "dateAired": ISODate("2021-08-02"),
        "listenedTo": true,
    },
    {
        "name": "Tech Over Tea",
        "episodeName": "Neckbeards Anonymous - Tech Over Tea #20 - feat Donald Feury",
        "dateAired": ISODate("2020-07-13"),
        "listenedTo": true
    },
    {
        "name": "Tech Over Tea",
        "episodeName": "#34 The Return Of The Clones - feat Bryan Jenks",
        "dateAired": ISODate("2020-10-19"),
        "listenedTo": false
    },
    {
        "name": "Cinemassacre Podcast",
        "episodeName": "AVGN Fan Q&amp;amp;A, Starting a Band, and the Last Year - Cinemassacre Podcast",
        "dateAired": ISODate("2021-08-10"),
        "listenedTo": true
    }
])

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's update all the "Tech Over Tea" podcasts entry to be marked as listened to using &lt;code&gt;update&lt;/code&gt;. In order to update multiple documents using &lt;code&gt;update&lt;/code&gt;, we have to pass in an optional argument to tell MongoDB to do that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.podcasts.update(
    {name: "Tech Over Tea"},
    {$set: { listenedTo: true} },
    {multi: true}
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can achieve the same result using &lt;code&gt;updateMany&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.podcasts.updateMany(
    {name: "Tech Over Tea"},
    {$set: { listenedTo: true} }
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>mongodb</category>
    </item>
    <item>
      <title>How to drop a collection from MongoDB</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Sun, 15 Aug 2021 14:20:51 +0000</pubDate>
      <link>https://dev.to/dak425/how-to-drop-a-collection-from-mongodb-3ieg</link>
      <guid>https://dev.to/dak425/how-to-drop-a-collection-from-mongodb-3ieg</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f6EwVQVr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://donaldfeury.xyz/content/images/2021/08/MongoDB_Logo2-2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f6EwVQVr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://donaldfeury.xyz/content/images/2021/08/MongoDB_Logo2-2.png" alt="How to drop a collection from MongoDB"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For a full overview of MongoDB and all my posts on it, check out my &lt;a href="https://donaldfeury.xyz/introduction-to-mongodb/"&gt;overview&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WARNING!&lt;/strong&gt; This is a dangerous command that will result in the loss of data&lt;/p&gt;

&lt;p&gt;If you want to completely drop a collection, which will result in the loss of all documents in that collection, there is one simple command to do that called &lt;code&gt;drop&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To drop a collection in your MongoDB instance called &lt;code&gt;podcasts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.podcasts.drop()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only do this if you're sure you want to completely reset that collection.&lt;/p&gt;

</description>
      <category>mongodb</category>
    </item>
    <item>
      <title>How to replace an existing document in MongoDB</title>
      <dc:creator>Donald Feury</dc:creator>
      <pubDate>Sat, 14 Aug 2021 15:41:40 +0000</pubDate>
      <link>https://dev.to/dak425/how-to-replace-an-existing-document-in-mongodb-137c</link>
      <guid>https://dev.to/dak425/how-to-replace-an-existing-document-in-mongodb-137c</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sXznSY1a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://donaldfeury.xyz/content/images/2021/08/MongoDB_Logo2-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sXznSY1a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://donaldfeury.xyz/content/images/2021/08/MongoDB_Logo2-1.png" alt="How to replace an existing document in MongoDB"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;For a full overview of MongoDB and all my posts on it, check out my &lt;a href="https://donaldfeury.xyz/introduction-to-mongodb/"&gt;overview&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://donaldfeury.xyz/how-to-update-a-single-document-into-a-mongodb-collection-2/"&gt;MongoDB provides several ways to update specifically one document&lt;/a&gt; that works great when doing partial updates.&lt;/p&gt;

&lt;p&gt;If you want to completely replace an existing document with a new one, you can use &lt;code&gt;replaceOne&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First, let's insert some data into a collection called &lt;code&gt;podcasts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.podcasts.insertMany([
    {
        "name": "Tech Over Tea",
        "episodeName": "#75 Welcome Our Hacker Neko Waifu | Cyan Nyan",
        "dateAired": ISODate("2021-08-02"),
        "listenedTo": true,
    },
    {
        "name": "Tech Over Tea",
        "episodeName": "Neckbeards Anonymous - Tech Over Tea #20 - feat Donald Feury",
        "dateAired": ISODate("2020-07-13"),
        "listenedTo": true
    },
    {
        "name": "Tech Over Tea",
        "episodeName": "#34 The Return Of The Clones - feat Bryan Jenks",
        "dateAired": ISODate("2020-10-19"),
        "listenedTo": false
    }
])

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's completely replace the podcast that aired on &lt;code&gt;2020-07-13&lt;/code&gt; with a new podcast. Unlike &lt;code&gt;update&lt;/code&gt; and &lt;code&gt;updateOne&lt;/code&gt;, &lt;code&gt;replaceOne&lt;/code&gt; does not use &lt;code&gt;update operators&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db.podcasts.replaceOne(
    {dateAired: ISODate("2020-07-13")},
    {
        "name": "Tech Over Tea",
        "episodeName": "#73 Is This A Gaming Podcast Now | Solo",
        "dateAired": ISODate("2021-07-19"),
        "listenedTo": false
    }
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The arguments are similar to &lt;code&gt;update&lt;/code&gt; and &lt;code&gt;updateOne&lt;/code&gt; where the first argument is the query to match a document to replace. However, the second argument is a new document that will completely replace the first matched document.&lt;/p&gt;

</description>
      <category>mongodb</category>
    </item>
  </channel>
</rss>
