<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>mongodb &amp;mdash; Simple Engineering</title>
    <link>https://getsimple.works/tag:mongodb</link>
    <description></description>
    <pubDate>Thu, 23 Apr 2026 14:44:29 +0000</pubDate>
    <item>
      <title>How to version mongodb models</title>
      <link>https://getsimple.works/how-to-version-mongodb-models?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[Sometimes, changes in code involves changes in models. Fields can be added or removed depending on the requirements at hand. This blog post explores some techniques to make versioning work with mongodb models. &#xA;&#xA;  There is a more generalist Database Maintenance, Data Migrations, and Model Versioning article that goes beyond mongodb models. &#xA;&#xA;In this article we will talk about: &#xA;&#xA;Model versioning strategies &#xA;Avoiding model versioning colliding with database engine upgrades&#xA;Migration strategy for model upgrades with schema change &#xA;Migration strategy for models with hydration&#xA;Tools that makes model migrations easier &#xA;&#xA;  Even though this blog post was designed to offer complementary materials to those who bought my Testing nodejs Applications book, the content can help any software developer to tuneup working environment. You use this link to buy the book.  Testing nodejs Applications Book Cover&#xA;&#xA;Show me the code&#xA;&#xA;These snippets illustrate the evolution of one fictitious UserSchema. A schema describes how a model will look like once compiled and ready to be used with the mongodb database engine. &#xA;&#xA;//Data Model Version 1.0.0&#xA;var UserSchema = new mongoose.Schema({name: String});&#xA;&#xA;//Data Model Version 1.0.1&#xA;var UserSchema = new mongoose.Schema({name: String, email: String});&#xA;&#xA;//Data Model Version 1.1.0&#xA;var UserSchema = new mongoose.Schema({&#xA;    name: String, &#xA;    email: {type: String, required: true}&#xA;});&#xA;&#xA;//Data Model Version 2.0.0&#xA;var UserSchema = new mongoose.Schema({ &#xA;    name: {first: String, last: String},&#xA;    addresses: [Address],&#xA;    orders: [Order]&#xA;});&#xA;&#xA;module.exports = mongoose.model(&#39;User&#39;, UserSchema);&#xA;Example: Evolution of a mongoose data model&#xA;&#xA;What can possibly go wrong?&#xA;&#xA;It common to execute software updates in a bulk, especially when the application is a monolith. The term bulk is used for lack of a better word, but the idea behind it can be resumed in a need to update data models, coupled with data hydration to new data models, with a potential of updating the database engine, all of those tasks at the same time.&#xA;&#xA;It becomes clear that, when we have to update more than two things at the same time, complex operations will get involved, and the more complex the update gets, the nastier the problem will become. &#xA;&#xA;When trying to figure out how to approach either migration from one model version to the next, from one low/high level ORM/ODM(mongoose, knex, sequelize) version to the next, from one database engine version to the next, or from one database driver version to the next, we should always keep in our mind some of these challenges(Questions):&#xA;&#xA;When is it the right time to do a migration&#xA;How to automate data transformations from one model version to the next &#xA;What is the difference between update, and upgrade in our particular context&#xA;What are the bottlenecks(moving parts) for the current database update/upgrade&#xA;How can we align model versioning, data migrations alongside database updates/upgrades/patches&#xA;&#xA;The key strategy to tackle difficult situations, at least in the context of these blog post series, has been to split big problems into sub-problems, then resolve one sub-problem at a time. &#xA;&#xA;Update vs Upgrade&#xA;&#xA;Database updates and patches are released on regular basis, they are safe and do not cause major problems when the time comes to apply them. From a system maintenance perspective, it makes sense to apply patches as soon as they come out, and on a regular repeatable basis. For example, every week Friday at midnight, a task can apply a patch to the database engine. At this point, there is one issue off our plate. How about database upgrades.&#xA;&#xA;Upgrades&#xA;&#xA;Avoiding model versioning colliding with other database-related upgrades ~ Any upgrade has breaking changes in it, some are minor others are really serious such as data format incompatibility and what-not. Since upgrades can cause harm, it makes sense to NOT do upgrades at the same time with model versioning, or worse, data model versioning. We may state the following upgrades: ORM/ODM, database driver, database engine upgrades. Since they are not frequent, they can be planned once every quarter depending on the schedule of software we are talking about. It makes sense to have a window to execute, test, and adapt if necessary. Once a quarter as a part of sprint cleaning makes more sense. As a precaution, it makes sense to NOT plan upgrades at the same time as model version changes. &#xA;&#xA;Model versioning strategies&#xA;&#xA;As expressed in the sample code, the evolution of data-driven applications goes hand in hand with schema evolution. As the application grows, some decisions are going to be detrimental and may also need corrective measures in further iterations. We keep in mind that some new features require revisiting schema. In all cases, the model schema will have to change to adapt to new realities. The complexity of schema change depends on how complex the addition or removal turns out to be. To reduce complexity and technical debt, every deployment should involve steps to update schema changes, and re-hydrate data into new models to reflect the new changes. When possible, features that require schema change can be moved to a minor(Major.Minor.Patch) release, whereas every day (in continuous delivery mode) release can be just patched. Similarly, the Major version releases can include ORM/ODM upgrades, database driver upgrades, database engine upgrades, data migration from an old system to the new system. It is NOT good to include model changes in the major, we can keep that in minor releases.  &#xA;&#xA;Migration strategy for model upgrades with schema change&#xA;&#xA;From previous sections, it makes sense to keep model upgrades, with schema change as a minor release task. And that, whether it implies data hydration or not. &#xA;&#xA;Migration strategy for models upgrades with data hydration&#xA;&#xA;Data hydration is necessary, in case the data structure has changed to remove fields, split fields, or adding embedded documents. Data hydration may not be necessary when schema change is relaxed validity or availability. However, if a field becomes required, then it makes sense to add a rehydration strategy. It is better to execute hydration every time there is a minor release, even when not necessary. &#xA;&#xA;Tools that make model upgrade easy&#xA;&#xA;There are some libraries that can be used to execute data migration/hydration as a part of model upgrade operation. node-migrate is one of them. Advanced tools on relational databases such as flywaydb can be used. When it comes to model upgrades, a consistent repeatable strategy pays more for your buck than a full-fledged solution in the wild.    &#xA;&#xA;Conclusion&#xA;&#xA;In this article, we revisited how to align schema versioning with mongodb releases, taking into consideration data migration and hydration, as well as tools to make data handling easier. There are additional complimentary materials in the &#34;Testing nodejs applications&#34; book. &#xA;&#xA;References&#xA;&#xA;Testing nodejs Applications book&#xA;&#xA;tags: #mongodb  #mongoose #migration #data-migration #model-migration #nodejs]]&gt;</description>
      <content:encoded><![CDATA[<p>Sometimes, changes in code involves changes in models. Fields can be added or removed depending on the requirements at hand. This blog post explores some techniques to make versioning work with <code>mongodb</code> models.</p>

<blockquote><p>There is a more generalist <a href="./database-maintenance-data-migrations-and-model-versioning">Database Maintenance, Data Migrations, and Model Versioning</a> article that goes beyond <code>mongodb</code> models.</p></blockquote>

<p><strong><em>In this article we will talk about:</em></strong></p>
<ul><li>Model versioning strategies</li>
<li>Avoiding model versioning colliding with database engine upgrades</li>
<li>Migration strategy for model upgrades with schema change</li>
<li>Migration strategy for models with hydration</li>
<li>Tools that makes model migrations easier</li></ul>

<blockquote><p>Even though this blog post was designed to offer complementary materials to those who bought my <strong><em><a href="https://bit.ly/2ZFJytb">Testing <code>nodejs</code> Applications book</a></em></strong>, the content can help any software developer to tuneup working environment. <strong><em><a href="https://bit.ly/2ZFJytb">You use this link to buy the book</a></em></strong>.  <a href="https://bit.ly/2ZFJytb"><img src="https://snap.as/a/42OS2vs.png" alt="Testing nodejs Applications Book Cover"/></a></p></blockquote>

<h2 id="show-me-the-code" id="show-me-the-code">Show me the code</h2>

<p>These snippets illustrate the evolution of one fictitious UserSchema. A schema describes how a model will look like once compiled and ready to be used with the <code>mongodb</code> database engine.</p>

<pre><code class="language-JavaScript">//Data Model Version 1.0.0
var UserSchema = new mongoose.Schema({name: String});

//Data Model Version 1.0.1
var UserSchema = new mongoose.Schema({name: String, email: String});

//Data Model Version 1.1.0
var UserSchema = new mongoose.Schema({
    name: String, 
    email: {type: String, required: true}
});

//Data Model Version 2.0.0
var UserSchema = new mongoose.Schema({ 
    name: {first: String, last: String},
    addresses: [Address],
    orders: [Order]
});

module.exports = mongoose.model(&#39;User&#39;, UserSchema);
</code></pre>

<p><em><em>Example</em>: Evolution of a mongoose data model</em></p>

<h2 id="what-can-possibly-go-wrong" id="what-can-possibly-go-wrong">What can possibly go wrong?</h2>

<p>It common to execute software updates in a bulk, especially when the application is a <em>monolith</em>. The term bulk is used for lack of a better word, but the idea behind it can be resumed in a need to update data models, coupled with data hydration to new data models, with a potential of updating the database engine, all of those tasks at the same time.</p>

<p>It becomes clear that, when we have to update more than two things at the same time, complex operations will get involved, and the more complex the update gets, the nastier the problem will become.</p>

<p>When trying to figure out how to approach either migration from one model version to the next, from one low/high level ORM/ODM(<code>mongoose</code>, <code>knex</code>, <code>sequelize</code>) version to the next, from one database engine version to the next, or from one database driver version to the next, we should always keep in our mind some of these challenges(Questions):</p>
<ul><li>When is it the right time to do a migration</li>
<li>How to automate data transformations from one model version to the next</li>
<li>What is the difference between update, and upgrade in our particular context</li>
<li>What are the bottlenecks(moving parts) for the current database update/upgrade</li>
<li>How can we align model versioning, data migrations alongside database updates/upgrades/patches</li></ul>

<p>The key strategy to tackle difficult situations, at least in the context of these blog post series, has been to split big problems into sub-problems, then resolve one sub-problem at a time.</p>

<h2 id="update-vs-upgrade" id="update-vs-upgrade">Update vs Upgrade</h2>

<p>Database updates and patches are released on regular basis, they are safe and do not cause major problems when the time comes to apply them. From a system maintenance perspective, it makes sense to apply patches as soon as they come out, and on a regular repeatable basis. For example, every week Friday at midnight, a task can apply a patch to the database engine. At this point, there is one issue off our plate. How about database upgrades.</p>

<h2 id="upgrades" id="upgrades">Upgrades</h2>

<p>Avoiding model versioning colliding with other database-related upgrades ~ Any upgrade has breaking changes in it, some are minor others are really serious such as data format incompatibility and what-not. Since upgrades can cause harm, it makes sense to NOT do upgrades at the same time with model versioning, or worse, data model versioning. We may state the following upgrades: ORM/ODM, database driver, database engine upgrades. Since they are not frequent, they can be planned once every quarter depending on the schedule of software we are talking about. It makes sense to have a window to execute, test, and adapt if necessary. Once a quarter as a part of sprint cleaning makes more sense. As a precaution, it makes sense to NOT plan upgrades at the same time as model version changes.</p>

<h2 id="model-versioning-strategies" id="model-versioning-strategies">Model versioning strategies</h2>

<p>As expressed in the sample code, the evolution of data-driven applications goes hand in hand with schema evolution. As the application grows, some decisions are going to be detrimental and may also need corrective measures in further iterations. We keep in mind that some new features require revisiting schema. In all cases, the model schema will have to change to adapt to new realities. The complexity of schema change depends on how complex the addition or removal turns out to be. To reduce complexity and technical debt, every deployment should involve steps to update schema changes, and re-hydrate data into new models to reflect the new changes. When possible, features that require schema change can be moved to a minor(Major.Minor.Patch) release, whereas every day (in continuous delivery mode) release can be just patched. Similarly, the Major version releases can include ORM/ODM upgrades, database driver upgrades, database engine upgrades, data migration from an old system to the new system. It is NOT good to include model changes in the major, we can keep that in minor releases.</p>

<h2 id="migration-strategy-for-model-upgrades-with-schema-change" id="migration-strategy-for-model-upgrades-with-schema-change">Migration strategy for model upgrades with schema change</h2>

<p>From previous sections, it makes sense to keep model upgrades, with schema change as a minor release task. And that, whether it implies data hydration or not.</p>

<h2 id="migration-strategy-for-models-upgrades-with-data-hydration" id="migration-strategy-for-models-upgrades-with-data-hydration">Migration strategy for models upgrades with data hydration</h2>

<p>Data hydration is necessary, in case the data structure has changed to remove fields, split fields, or adding embedded documents. Data hydration may not be necessary when schema change is relaxed validity or availability. However, if a field becomes required, then it makes sense to add a rehydration strategy. It is better to execute hydration every time there is a minor release, even when not necessary.</p>

<h2 id="tools-that-make-model-upgrade-easy" id="tools-that-make-model-upgrade-easy">Tools that make model upgrade easy</h2>

<p>There are some libraries that can be used to execute data migration/hydration as a part of model upgrade operation. <a href="https://github.com/tj/node-migrate"><code>node-migrate</code></a> is one of them. Advanced tools on relational databases such as <a href="https://flywaydb.org/"><code>flywaydb</code></a> can be used. When it comes to model upgrades, a consistent repeatable strategy pays more for your buck than a full-fledged solution in the wild.</p>

<h2 id="conclusion" id="conclusion">Conclusion</h2>

<p>In this article, we revisited how to align schema versioning with <code>mongodb</code> releases, taking into consideration data migration and hydration, as well as tools to make data handling easier. There are additional complimentary materials in the <strong>“Testing <code>nodejs</code> applications”</strong> book.</p>

<h2 id="references" id="references">References</h2>
<ul><li><a href="https://bit.ly/2ZFJytb">Testing <code>nodejs</code> Applications book</a></li></ul>

<p>tags: <a href="https://getsimple.works/tag:mongodb" class="hashtag"><span>#</span><span class="p-category">mongodb</span></a>  <a href="https://getsimple.works/tag:mongoose" class="hashtag"><span>#</span><span class="p-category">mongoose</span></a> <a href="https://getsimple.works/tag:migration" class="hashtag"><span>#</span><span class="p-category">migration</span></a> <a href="https://getsimple.works/tag:data" class="hashtag"><span>#</span><span class="p-category">data</span></a>-migration <a href="https://getsimple.works/tag:model" class="hashtag"><span>#</span><span class="p-category">model</span></a>-migration <a href="https://getsimple.works/tag:nodejs" class="hashtag"><span>#</span><span class="p-category">nodejs</span></a></p>
]]></content:encoded>
      <guid>https://getsimple.works/how-to-version-mongodb-models</guid>
      <pubDate>Thu, 17 Jun 2021 06:06:37 +0000</pubDate>
    </item>
    <item>
      <title>How to install mongodb</title>
      <link>https://getsimple.works/how-to-install-mongodb?pk_campaign=rss-feed</link>
      <description>&lt;![CDATA[This article revisits essentials on how to install mongodb, one of leading noSQL databases on development and production servers.&#xA;&#xA;  This article has complementary materials to the Testing nodejs Applications book. However, the article is designed to help those who bought the book to setup their working environment, as well as the wide audience of software developers who need same information.  &#xA;&#xA;In this article we will talk about:&#xA;&#xA;How to install mongodb on Linux, macOS and Windows.&#xA;How to stop/start automatically prior/after system restarts&#xA;&#xA;We will not talk about:&#xA;&#xA;How to configure mongodb for development and production, as that is subject of another article worth visiting. &#xA;How to manage mongodb in a production environment, either in a containerized or standalone contexts.&#xA;How to load mongodb on Docker and Kubernetes&#xA;&#xA;Installing mongodb on Linux &#xA;&#xA;It is always a good idea to update the system before start working. There is no exception, even when a daily task updates automatically binaries. That can be achieved on Ubuntu and Aptitude enabled systems as following:&#xA;&#xA;$ apt-get update        # Fetch list of available updates&#xA;$ apt-get upgrade       # Upgrades current packages&#xA;$ apt-get dist-upgrade  # Installs only new updates&#xA;Example: updating aptitude binaries&#xA;&#xA;At this point most of packages should be installed or upgraded. Except Packages whose PPA have been removed or not available in the registry. Installing software can be done by installing binaries, or using Ubuntu package manager.&#xA;&#xA;Installing a mongodb on Linux using apt&#xA;&#xA;Updating/Upgrading or first time fresh install of &#xA;  sudo may be skipped if the current user has permission to write and execute programs &#xA;&#xA;Add public key used by the aptitude for further updates&#xA;gnupg should be available in the system&#xA;$ apt-get install gnupg &#xA;$ wget -qO - https://www.mongodb.org/static/pgp/server-3.6.asc | sudo apt-key add - &#xA;&#xA;create and add list for mongodb (version 3.6, but variations can differ from version to version, the same applies to architecture)&#xA;$ echo &#34;deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 multiverse&#34; | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list&#xA;&#xA;Updating libraries and make the actual install &#xA;$ sudo apt-get update&#xA;$ sudo apt-get install -y mongodb-org&#xA;&#xA;To install specific version(3.6.17 in our example) of mongodb, the following command helps&#xA;$ sudo apt-get install -y mongodb-org=3.6.17 mongodb-org-server=3.6.17 mongodb-org-shell=3.6.17 mongodb-org-mongos=3.6.17 mongodb-org-tools=3.6.17&#xA;Example: adding mongodb PPA binaries and installing a particular binary version&#xA;&#xA;It is always a good idea to upgrade often. Breaking changes happen on major/minor binary updates, but less likely on patch upgrades. The versions goes by pair numbers, so 3.2, 3.4, 3.6 etc. The transition that skips two version may be catastrophic. For example  upgrades from any 3.x to 3.6, for this to work, there should be upgraded to an intermediate update from 3.x to 3.4, after which the update from 3.4 to 3.6 becomes possible. &#xA;&#xA;Part 1&#xA;$ apt-cache policy mongodb-org          # Checking installed MongoDB version &#xA;$ apt-get install -y mongodb-org=3.4    # Installing 3.4 MongoDB version &#xA;&#xA;Part 2   &#xA;Running mongodb&#xA;$ sudo killall mongod &amp;&amp; sleep 3 &amp;&amp; sudo service mongod start&#xA;$ sudo service mongodb start           &#xA;&#xA;Part 3 &#xA;$ mongo                                 # Accessing to mongo CLI&#xA;&#xA;Compatible Mode&#xA;$   db.adminCommand( { setFeatureCompatibilityVersion: &#34;3.4&#34; } )  &#xA;$   exit&#xA;&#xA;Part 3 &#xA;$ sudo apt-get install -y mongodb-org=3.6   # Upgrading to latest 3.6 version &#xA;Restart Server + As in Part 2.&#xA;Example: updating mongodb binaries and upgrading to a version&#xA;&#xA;Installing mongodb on macOS &#xA;&#xA;In case homebrew is not already available on your mac, this is how to get one up and running. On its own, homebrew depends on ruby runtime to be available. &#xA;&#xA;  homebrew is a package manager and software installation tool that makes most developer tools installation a breeze. We should also highlight that homebrew requires xcode to be available on the system. &#xA;&#xA;$ /usr/bin/ruby -e \&#xA;    &#34;$(curl -fsSL https://raw.githubusercontent.com \&#xA;    /Homebrew/install/master/install)&#34;&#xA;Example: installation instruction as provided by brew.sh&#xA;&#xA;Generally speaking, this is how to install and uninstall things with brew &#xA;&#xA;$ brew install wget &#xA;$ brew uninstall wget &#xA;Example: installing/uninstalling wget binaries using homebrew&#xA;&#xA;  We have to to stress on the fact that Homebrew installs packages to their own directory and then symlinks their files into /usr/local.&#xA;&#xA;It is always a good idea to update the system before start working. And that, even when we have a daily task that automatically updates the system for us. macOS  can use homebrew package manager on maintenance matters. To update/upgrade or check outdated packages, following commands would help. &#xA;&#xA;$ brew outdated                   # lists all outdated packages&#xA;$ brew cleanup -n                 # visualize the list of things are going to be cleaned up.&#xA;&#xA;$ brew upgrade                    # Upgrades all things on the system&#xA;$ brew update                     # Updates all outdated + brew itself&#xA;$ brew update formula           # Updates one formula&#xA;&#xA;$ brew install formula@version    # Installs formula at a particular version.&#xA;$ brew tap formular@version/brew  # Installs formular from third party repository&#xA;&#xA;untap/re-tap a repo when previous installation failed&#xA;$ brew untap formular &amp;&amp; brew tap formula   &#xA;$ brew services start formular@version&#xA;Example: key commands to work with homebrew cli&#xA;&#xA;  For more informations, visit: Homebrew ~ FAQ.&#xA;&#xA;Installing a mongodb on a Mac using homebrew&#xA;&#xA;$ brew tap mongodb/brew &#xA;$ brew install mongodb-community@3.6&#xA;$ brew services start mongodb-community@3.6 # start mongodb as a mac service &#xA;Example: Install and running mongodb as a macOS service&#xA;&#xA;  Caveats ~ We have extra steps to make in order to start/stop automatically when the system goes up/down. This step is vital when doing development on macOS , which does not necessarily needs Linux production bound task runners.&#xA;&#xA;To have launchd start mongodb at login:&#xA;$ ln -s /usr/local/opt/mongodb/.plist ~/Library/LaunchAgents/&#xA;&#xA;Then to load mongodb now:&#xA;$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist&#xA;&#xA;To unregister and stop the service, use the following command&#xA;$ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist&#xA;&#xA;When not want/need launchctl this command works fine&#xA;$ mongod &#xA;Example: Stop/Start when the system stops/starts&#xA;&#xA;Installing mongodb on a Windows machine&#xA;&#xA;Whereas MacOs, and most Linux distributions, come with Python and Ruby already enabled, It takes extra mile for Windows to make those two languages available. We have to stress on the fact that those two languages are somehow required to deploy a mongodb environment on most platforms, especially when working with nodejs. &#xA;&#xA;The bright side of this story is that mongodb provides windows binaries that we can downloaded and installed in a couple of clicks.&#xA;&#xA;Automated upgrades &#xA;&#xA;Before we dive into automatic upgrades, we should consider nuances associated to managing a mongodb instance. The updates fall into two major, quite interesting, categories: patch updates and version upgrades. &#xA;&#xA;Following the SemVer ~ aka Semantic Versioning standard, it is not recommended to consider minor/major versions for automated upgrades. One of the reasons being that these versions are subject to introducing breaking changes or incompatibility between two versions.  On the other hand, patches are less susceptible to introduce breaking changes, whence ideal candidates for automated upgrades. Another among other reasons, being that security fixes are released as patches to a minor version.  &#xA;&#xA;  We should highlight that it is always better to upgrade at deployment time. The process is even easier in containerized context. We should also automate only patches, to avoid to miss security patches. &#xA;&#xA;In the context of Linux, we will use the unattended-upgrades package to do the work. &#xA;&#xA;$ apt-get install unattended-upgrades apticron&#xA;Example: install unattended-upgrades&#xA;&#xA;Two things to fine-tune to make this solution work are: to enable a blacklist of packages we do not to automatically update, and two, to enable particular packages we would love to update on a periodical basis. That is compiled in the following shell scripts.&#xA;&#xA;Unattended-Upgrade::Allowed-Origins {&#xA;//  &#34;${distroid}:${distrocodename}&#34;;&#xA;    &#34;${distroid}:${distrocodename}-security&#34;; # upgrading security patches only &#xA;//   &#34;${distroid}:${distrocodename}-updates&#34;;  &#xA;//  &#34;${distroid}:${distrocodename}-proposed&#34;;&#xA;//  &#34;${distroid}:${distrocodename}-backports&#34;;&#xA;};&#xA;&#xA;Unattended-Upgrade::Package-Blacklist {&#xA;    &#34;vim&#34;;&#xA;};&#xA;Example: fine-tune the blacklist and whitelist in /etc/apt/apt.conf.d/50unattended-upgrades&#xA;&#xA;The next step is necessary to make sure  unattended-upgrades download, install and cleanups tasks have a default period: once, twice a day or a week. &#xA;&#xA;APT::Periodic::Update-Package-Lists &#34;1&#34;;            # Updates package list once a day&#xA;APT::Periodic::Download-Upgradeable-Packages &#34;1&#34;;   # download upgrade candidates once a day&#xA;APT::Periodic::AutocleanInterval &#34;7&#34;;               # clean week worth of unused packages once a week&#xA;APT::Periodic::Unattended-Upgrade &#34;1&#34;;              # install downloaded packages once a day&#xA;Example: tuning the tasks parameter /etc/apt/apt.conf.d/20auto-upgrades&#xA;&#xA;This approach works on Linux(Ubuntu), especially deployed in production, but not Windows nor macOS. The last issue, is to be able to report problems when an update fails, so that a human can intervene whenever possible. That is where the second tool apticron in first paragraph intervenes. To make it work, we will specify which email to send messages to, and that will be all. &#xA;&#xA;EMAIL=&#34;email@host.tld&#34;&#xA;Example: tuning reporting tasks email parameter /etc/apticron/apticron.conf&#xA;&#xA;Conclusion&#xA;&#xA;In this article we revisited ways to install mongodb on various platforms. Even though configuration was beyond the scope of this article*, we managed to get everyday quick refreshers out in the article. There are areas we wish we added more coverage, probably not on the first version of this article. &#xA;&#xA;  Some of other places where people are wondering how to install mongodb on various platforms include, but not limited to Quora, StackOverflow and Reddit. &#xA;&#xA;Reading list &#xA;&#xA;Documentation on how to Install mongodb on Ubuntu&#xA; An A-Z Index of the Apple macOS command line (macOS bash) and the Apple macOS How-to guides and examples, launchd&#xA; The answer to two questions: 1) How to know package version and 2) How to install a specific Package with aptitude. &#xA;Performing Automated In-place Cluster Updates, on Google Cloud Platform: Auto-upgrading nodes&#xA;More on configuring unattended-upgrades are in these articles: Automatic Updates,  How to Enable Unattended Upgrades on Ubuntu/Debian and How to Setup Automatic Security Updates on Ubuntu 16.04&#xA;&#xA;#nodejs #homebrew #UnattendedUpgrades #mongodb #y2020 #Jan2020 #HowTo #ConfiguringNodejsApplications #tdd #TestingNodejsApplications]]&gt;</description>
      <content:encoded><![CDATA[<p>This article revisits essentials on how to install <code>mongodb</code>, one of leading noSQL databases on development and production servers.</p>

<blockquote><p>This article has complementary materials to the <strong><em><a href="http://bit.ly/2ZFJytb">Testing <code>nodejs</code> Applications book</a></em></strong>. However, the article is designed to help those who bought the book to setup their working environment, as well as the wide audience of software developers who need same information.</p></blockquote>

<p><strong>In this article we will talk about</strong>:</p>

<p><strong>__</strong></p>
<ul><li>How to install <code>mongodb</code> on Linux, macOS and Windows.</li>
<li>How to stop/start automatically prior/after system restarts</li></ul>

<p><strong>We will not talk about</strong>:</p>

<p><strong>__</strong></p>
<ul><li>How to configure <code>mongodb</code> for development and production, as that is subject of another article worth visiting.</li>
<li>How to manage <code>mongodb</code> in a production environment, either in a containerized or standalone contexts.</li>
<li>How to load <code>mongodb</code> on Docker and Kubernetes</li></ul>

<h2 id="installing-mongodb-on-linux" id="installing-mongodb-on-linux">Installing <code>mongodb</code> on Linux</h2>

<p>It is always a good idea to update the system before start working. There is no exception, even when a daily task updates automatically binaries. That can be achieved on Ubuntu and Aptitude enabled systems as following:</p>

<pre><code class="language-shell">$ apt-get update        # Fetch list of available updates
$ apt-get upgrade       # Upgrades current packages
$ apt-get dist-upgrade  # Installs only new updates
</code></pre>

<p><em><em>Example</em>: updating aptitude binaries</em></p>

<p>At this point most of packages should be installed or upgraded. Except Packages whose PPA have been removed or not available in the registry. Installing software can be done by installing binaries, or using Ubuntu package manager.</p>

<h3 id="installing-a-mongodb-on-linux-using-apt" id="installing-a-mongodb-on-linux-using-apt">Installing a <code>mongodb</code> on Linux using <code>apt</code></h3>

<p>Updating/Upgrading or first time fresh install of <code>mongodb</code> can follow next scripts.</p>

<blockquote><p><strong><em><code>sudo</code></em></strong> may be skipped if the current user has permission to write and execute programs</p></blockquote>

<pre><code class="language-shell"># Add public key used by the aptitude for further updates
# gnupg should be available in the system
$ apt-get install gnupg 
$ wget -qO - https://www.mongodb.org/static/pgp/server-3.6.asc | sudo apt-key add - 

# create and add list for mongodb (version 3.6, but variations can differ from version to version, the same applies to architecture)
$ echo &#34;deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.6 multiverse&#34; | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

# Updating libraries and make the actual install 
$ sudo apt-get update
$ sudo apt-get install -y mongodb-org

# To install specific version(3.6.17 in our example) of mongodb, the following command helps
$ sudo apt-get install -y mongodb-org=3.6.17 mongodb-org-server=3.6.17 mongodb-org-shell=3.6.17 mongodb-org-mongos=3.6.17 mongodb-org-tools=3.6.17
</code></pre>

<p><em><em>Example</em>: adding <code>mongodb</code> PPA binaries and installing a particular binary version</em></p>

<p>It is always a good idea to upgrade often. Breaking changes happen on major/minor binary updates, but less likely on patch upgrades. The versions goes by pair numbers, so 3.2, 3.4, 3.6 etc. The transition that skips two version may be catastrophic. For example  upgrades from any 3.x to 3.6, for this to work, there should be upgraded to an intermediate update from 3.x to 3.4, after which the update from 3.4 to 3.6 becomes possible.</p>

<pre><code class="language-shell"># Part 1
$ apt-cache policy mongodb-org          # Checking installed MongoDB version 
$ apt-get install -y mongodb-org=3.4    # Installing 3.4 MongoDB version 

# Part 2   
# Running mongodb
$ sudo killall mongod &amp;&amp; sleep 3 &amp;&amp; sudo service mongod start
$ sudo service mongodb start           

# Part 3 
$ mongo                                 # Accessing to mongo CLI

# Compatible Mode
$ &gt; db.adminCommand( { setFeatureCompatibilityVersion: &#34;3.4&#34; } )  
$ &gt; exit

# Part 3 
$ sudo apt-get install -y mongodb-org=3.6   # Upgrading to latest 3.6 version 
# Restart Server + As in Part 2.
</code></pre>

<p><em><em>Example</em>: updating <code>mongodb</code> binaries and upgrading to a version</em></p>

<h2 id="installing-mongodb-on-macos" id="installing-mongodb-on-macos">Installing <code>mongodb</code> on macOS</h2>

<p>In case <code>homebrew</code> is not already available on your mac, this is how to get one up and running. On its own, <code>homebrew</code> depends on ruby runtime to be available.</p>

<blockquote><p><code>homebrew</code> is a package manager and software installation tool that makes most developer tools installation a breeze. We should also highlight that <code>homebrew</code> requires <code>xcode</code> to be available on the system.</p></blockquote>

<pre><code class="language-shell">$ /usr/bin/ruby -e \
    &#34;$(curl -fsSL https://raw.githubusercontent.com \
    /Homebrew/install/master/install)&#34;
</code></pre>

<p><em><em>Example:</em> installation instruction as provided by <a href="https://brew.sh/">brew.sh</a></em></p>

<p>Generally speaking, this is how to <strong><em>install</em></strong> and <strong><em>uninstall</em></strong> things with <code>brew</code></p>

<pre><code class="language-shell">$ brew install wget 
$ brew uninstall wget 
</code></pre>

<p><em><em>Example</em>: installing/uninstalling <code>wget</code> binaries using homebrew</em></p>

<blockquote><p>We have to to stress on the fact that <a href="https://brew.sh/">Homebrew</a> installs packages to their own directory and then symlinks their files into <code>/usr/local</code>.</p></blockquote>

<p>It is always a good idea to update the system before start working. And that, even when we have a daily task that automatically updates the system for us. macOS  can use <code>homebrew</code> package manager on maintenance matters. To update/upgrade or check outdated packages, following commands would help.</p>

<pre><code class="language-shell">$ brew outdated                   # lists all outdated packages
$ brew cleanup -n                 # visualize the list of things are going to be cleaned up.

$ brew upgrade                    # Upgrades all things on the system
$ brew update                     # Updates all outdated + brew itself
$ brew update &lt;formula&gt;           # Updates one formula

$ brew install &lt;formula@version&gt;    # Installs &lt;formula&gt; at a particular version.
$ brew tap &lt;formular@version&gt;/brew  # Installs &lt;formular&gt; from third party repository

# untap/re-tap a repo when previous installation failed
$ brew untap &lt;formular&gt; &amp;&amp; brew tap &lt;formula&gt;   
$ brew services start &lt;formular&gt;@&lt;version&gt;
</code></pre>

<p><em><em>Example</em>: key commands to work with <code>homebrew</code> cli</em></p>

<blockquote><p>For more informations, visit: <a href="https://docs.brew.sh/FAQ">Homebrew ~ FAQ</a>.</p></blockquote>

<h3 id="installing-a-mongodb-on-a-mac-using-homebrew" id="installing-a-mongodb-on-a-mac-using-homebrew">Installing a <code>mongodb</code> on a Mac using <code>homebrew</code></h3>

<pre><code class="language-shell">$ brew tap mongodb/brew 
$ brew install mongodb-community@3.6
$ brew services start mongodb-community@3.6 # start mongodb as a mac service 
</code></pre>

<p><em><em>Example</em>: Install and running <code>mongodb</code> as a macOS service</em></p>

<blockquote><p><strong><em>Caveats</em></strong> ~ We have extra steps to make in order to start/stop automatically when the system goes up/down. This step is vital when doing development on macOS , which does not necessarily needs Linux production bound task runners.</p></blockquote>

<pre><code class="language-shell"># To have launchd start mongodb at login:
$ ln -s /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents/

# Then to load mongodb now:
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

# To unregister and stop the service, use the following command
$ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

# When not want/need launchctl this command works fine
$ mongod 
</code></pre>

<p><em><em>Example</em>: Stop/Start when the system stops/starts</em></p>

<h2 id="installing-mongodb-on-a-windows-machine" id="installing-mongodb-on-a-windows-machine">Installing <code>mongodb</code> on a Windows machine</h2>

<p>Whereas MacOs, and most Linux distributions, come with Python and Ruby already enabled, It takes extra mile for Windows to make those two languages available. We have to stress on the fact that those two languages are somehow required to deploy a <code>mongodb</code> environment on most platforms, especially when working with <code>nodejs</code>.</p>

<p>The bright side of this story is that <a href="https://www.mongodb.com/download-center/community"><code>mongodb</code></a> provides windows binaries that we can downloaded and installed in a couple of clicks.</p>

<h2 id="automated-upgrades" id="automated-upgrades">Automated upgrades</h2>

<p>Before we dive into automatic upgrades, we should consider nuances associated to managing a <code>mongodb</code> instance. The updates fall into two major, quite interesting, categories: <strong><em>patch</em></strong> updates and <strong><em>version upgrades</em></strong>.</p>

<p>Following the <a href="https://semver.org/">SemVer ~ <em>aka Semantic Versioning</em></a> standard, it is not recommended to consider <strong><em>minor</em></strong>/<strong><em>major</em></strong> versions for automated upgrades. One of the reasons being that these versions are subject to introducing breaking changes or incompatibility between two versions.  On the other hand, patches are less susceptible to introduce breaking changes, whence ideal candidates for automated upgrades. Another among other reasons, being that security fixes are released as patches to a minor version.</p>

<blockquote><p>We should highlight that it is always better to upgrade at deployment time. The process is even easier in containerized context. We should also automate only patches, to avoid to miss security patches.</p></blockquote>

<p>In the context of Linux, we will use the <strong><em>unattended-upgrades</em></strong> package to do the work.</p>

<pre><code class="language-shell">$ apt-get install unattended-upgrades apticron
</code></pre>

<p><em><em>Example</em>: install unattended-upgrades</em></p>

<p>Two things to fine-tune to make this solution work are: to enable a blacklist of packages we do not to automatically update, and two, to enable particular packages we would love to update on a periodical basis. That is compiled in the following shell scripts.</p>

<pre><code class="language-shell">Unattended-Upgrade::Allowed-Origins {
//  &#34;${distro_id}:${distro_codename}&#34;;
    &#34;${distro_id}:${distro_codename}-security&#34;; # upgrading security patches only 
//   &#34;${distro_id}:${distro_codename}-updates&#34;;  
//  &#34;${distro_id}:${distro_codename}-proposed&#34;;
//  &#34;${distro_id}:${distro_codename}-backports&#34;;
};

Unattended-Upgrade::Package-Blacklist {
    &#34;vim&#34;;
};
</code></pre>

<p><em><em>Example</em>: fine-tune the blacklist and whitelist in <code>/etc/apt/apt.conf.d/50unattended-upgrades</code></em></p>

<p>The next step is necessary to make sure  <strong><em>unattended-upgrades</em></strong> download, install and cleanups tasks have a default period: once, twice a day or a week.</p>

<pre><code class="language-shell">APT::Periodic::Update-Package-Lists &#34;1&#34;;            # Updates package list once a day
APT::Periodic::Download-Upgradeable-Packages &#34;1&#34;;   # download upgrade candidates once a day
APT::Periodic::AutocleanInterval &#34;7&#34;;               # clean week worth of unused packages once a week
APT::Periodic::Unattended-Upgrade &#34;1&#34;;              # install downloaded packages once a day
</code></pre>

<p><em><em>Example</em>: tuning the tasks parameter <code>/etc/apt/apt.conf.d/20auto-upgrades</code></em></p>

<p>This approach works on Linux(Ubuntu), especially deployed in production, but not Windows nor macOS. The last issue, is to be able to report problems when an update fails, so that a human can intervene whenever possible. That is where the second tool <code>apticron</code> in first paragraph intervenes. To make it work, we will specify which email to send messages to, and that will be all.</p>

<pre><code class="language-shell">EMAIL=&#34;&lt;email&gt;@&lt;host.tld&gt;&#34;
</code></pre>

<p><em><em>Example</em>: tuning reporting tasks email parameter <code>/etc/apticron/apticron.conf</code></em></p>

<h2 id="conclusion" id="conclusion">Conclusion</h2>

<p>In this article we revisited ways to install <code>mongodb</code> on various platforms. Even though <em><a href="https://getsimple.works/how-to-configure-nodejs-applications#configure-mongodb-as-a-database-server-for-nodejs-project">configuration was beyond the scope of this article</a></em>, we managed to get everyday quick refreshers out in the article. There are areas we wish we added more coverage, probably not on the first version of this article.</p>

<blockquote><p>Some of other places where people are wondering how to install <code>mongodb</code> on various platforms include, but not limited to <a href="https://www.quora.com/search?q=install+mongodb">Quora</a>, <a href="https://stackoverflow.com/search?q=install+mongodb">StackOverflow</a> and <a href="https://www.reddit.com/search?q=install%20mongodb&amp;restrict_sr=">Reddit</a>.</p></blockquote>

<h2 id="reading-list" id="reading-list">Reading list</h2>
<ul><li>Documentation on how to <a href="https://docs.mongodb.com/v3.6/tutorial/install-mongodb-on-ubuntu/">Install <code>mongodb</code> on Ubuntu</a>
<ul><li><em><a href="https://ss64.com/osx/">An A-Z Index of the Apple macOS command line (macOS bash)</a></em> and the <em><a href="https://ss64.com/osx/syntax.html">Apple macOS How-to guides and examples</a></em>, <a href="https://www.launchd.info">launchd</a></li>
<li><a href="https://askubuntu.com/a/428778">The answer to two questions</a>: 1) <strong><em>How to know package version</em></strong> and 2) <strong><em>How to install a specific Package with aptitude</em></strong>.</li></ul></li>
<li><a href="https://docs.openshift.com/container-platform/3.6/install_config/upgrading/automated_upgrades.html">Performing Automated In-place Cluster Updates</a>, on Google Cloud Platform: <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/node-auto-upgrades">Auto-upgrading nodes</a></li>
<li>More on configuring unattended-upgrades are in these articles: <a href="https://help.ubuntu.com/lts/serverguide/automatic-updates.html">Automatic Updates</a>,  <a href="https://haydenjames.io/how-to-enable-unattended-upgrades-on-ubuntu-debian/">How to Enable Unattended Upgrades on Ubuntu/Debian</a> and <a href="https://www.howtoforge.com/tutorial/how-to-setup-automatic-security-updates-on-ubuntu-1604/">How to Setup Automatic Security Updates on Ubuntu 16.04</a></li></ul>

<p><a href="https://getsimple.works/tag:nodejs" class="hashtag"><span>#</span><span class="p-category">nodejs</span></a> <a href="https://getsimple.works/tag:homebrew" class="hashtag"><span>#</span><span class="p-category">homebrew</span></a> <a href="https://getsimple.works/tag:UnattendedUpgrades" class="hashtag"><span>#</span><span class="p-category">UnattendedUpgrades</span></a> <a href="https://getsimple.works/tag:mongodb" class="hashtag"><span>#</span><span class="p-category">mongodb</span></a> <a href="https://getsimple.works/tag:y2020" class="hashtag"><span>#</span><span class="p-category">y2020</span></a> <a href="https://getsimple.works/tag:Jan2020" class="hashtag"><span>#</span><span class="p-category">Jan2020</span></a> <a href="https://getsimple.works/tag:HowTo" class="hashtag"><span>#</span><span class="p-category">HowTo</span></a> <a href="https://getsimple.works/tag:ConfiguringNodejsApplications" class="hashtag"><span>#</span><span class="p-category">ConfiguringNodejsApplications</span></a> <a href="https://getsimple.works/tag:tdd" class="hashtag"><span>#</span><span class="p-category">tdd</span></a> <a href="https://getsimple.works/tag:TestingNodejsApplications" class="hashtag"><span>#</span><span class="p-category">TestingNodejsApplications</span></a></p>
]]></content:encoded>
      <guid>https://getsimple.works/how-to-install-mongodb</guid>
      <pubDate>Fri, 31 Jan 2020 22:47:36 +0000</pubDate>
    </item>
  </channel>
</rss>