all repos — gemini-redirect @ 4e76ffb372acbefd1eed19ac74caf7f564084240

blog/ribw/mongodb-an-introduction/index.html (view raw)

  1<!DOCTYPE html>
  2<html>
  3<head>
  4<meta charset="utf-8" />
  5<meta name="viewport" content="width=device-width, initial-scale=1" />
  6<title>MongoDB: an Introduction</title>
  7<link rel="stylesheet" href="../css/style.css">
  8</head>
  9<body>
 10<main>
 11<p>This is the first post in the MongoDB series, where we will introduce the MongoDB database system and take a look at its features and installation methods.</p>
 12<div class="date-created-modified">Created 2020-03-05<br>
 13Modified 2020-04-08</div>
 14<p>Other posts in this series:</p>
 15<ul>
 16<li><a href="/blog/ribw/mongodb-an-introduction/">MongoDB: an Introduction</a> (this post)</li>
 17<li><a href="/blog/ribw/mongodb-basic-operations-and-architecture/">MongoDB: Basic Operations and Architecture</a></li>
 18<li><a href="/blog/ribw/developing-a-python-application-for-mongodb/">Developing a Python application for MongoDB</a></li>
 19</ul>
 20<p>This post is co-authored wih Classmate.</p>
 21<hr />
 22<div class="image-container">
 23<img src="mongodb.png" alt="NoSQL database – MongoDB – First delivery" />
 24<div class="image-caption"></div>
 25</div>
 26<p>
 27<h2 class="title" id="purpose_of_technology"><a class="anchor" href="#purpose_of_technology">¶</a>Purpose of technology</h2>
 28<p>MongoDB is a <strong>general purpose, document-based, distributed database</strong> built for modern application developers and for the cloud era, with the scalability and flexibility that you want with the querying and indexing that you need. It being a document database means it stores data in JSON-like documents.</p>
 29<p>The Mongo team believes this is the most natural way to think about data, which is (they claim) much more expressive and powerful than the traditional row/column model, since programmers think in objects.</p>
 30<h2 id="how_it_works"><a class="anchor" href="#how_it_works">¶</a>How it works</h2>
 31<p>MongoDB’s architecture can be summarized as follows:</p>
 32<ul>
 33<li>Document data model.</li>
 34<li>Distributed systems design.</li>
 35<li>Unified experience with freedom to run it anywhere.</li>
 36</ul>
 37<p>For a more in-depth explanation, MongoDB offers a <a href="https://www.mongodb.com/collateral/mongodb-architecture-guide">download to the MongoDB Architecture Guide</a> with roughly ten pages worth of text.</p>
 38<p><img src="knGHenfTGA4kzJb1PHmS9EQvtZl2QlhbIPN15M38m8fZfZf7ODwYfhf0Tltr.png" alt="" />
 39_ Overview of MongoDB’s architecture_</p>
 40<p>Regarding usage, MongoDB comes with a really nice introduction along with JavaScript, Python, Java, C++ or C# code at our choice, which describes the steps necessary to make it work. Below we will describe a common workflow.</p>
 41<p>First, we must <strong>connect</strong> to a running MongoDB instance. Once the connection succeeds, we can access individual «collections», which we can think of as <em>tables</em> where collections of data is stored.</p>
 42<p>For instance, we could <strong>insert</strong> an arbitrary JSON document into the <code>restaurants</code> collection to store information about a restaurant.</p>
 43<p>At any other point in time, we can <strong>query</strong> these collections. The queries range from trivial, empty ones (which would retrieve all the documents and fields) to more rich and complex queries (for instance, using AND and OR operators, checking if data exists, and then looking for a value in a list).</p>
 44<p>MongoDB also supports the creation of <strong>indices</strong>, similar to those in other database systems. It allows for the creation of indices on any field or subfields.</p>
 45<p>In Mongo, the <strong>aggregation pipeline</strong> allows us to filter and analyze data based on a given set of criteria. For example, we could pull all the documents in the <code>restaurants</code> collection that have a <code>category</code> of <code>Bakery</code> using the <code>$match</code> operator. Then, we can group them by their star rating using the <code>$group</code> operator. Using the accumulator operator, <code>$sum</code>, we can see how many bakeries in our collection have each star rating.</p>
 46<h2 id="features"><a class="anchor" href="#features">¶</a>Features</h2>
 47<p>The features can be seen all over the place in their site, because it’s something they make a lot of emphasis on:</p>
 48<ul>
 49<li>
 50<p><strong>Easy development</strong>, thanks to the document data model, something they claim to be «the best way to work with data».</p>
 51</li>
 52<li>
 53<p>Data is stored in flexible JSON-like documents.</p>
 54</li>
 55<li>
 56<p>This model directly maps to the objects in the application’s code.</p>
 57</li>
 58<li>
 59<p>Ad hoc queries, indexing, and real time aggregation provide powerful ways to access and analyze the data.</p>
 60</li>
 61<li>
 62<p><strong>Powerful query language</strong>, with a rich and expressive query language that allows filtering and sorting by any field, no matter how nested it may be within a document. The queries are themselves JSON, and thus easily composable.</p>
 63</li>
 64<li>
 65<p><strong>Support for aggregations</strong> and other modern use-cases such as geo-based search, graph search, and text search.</p>
 66</li>
 67<li>
 68<p><strong>A distributed systems design</strong>, which allows developers to intelligently put data where they want it. High availability, horizontal scaling, and geographic distribution are built in and easy to use.</p>
 69</li>
 70<li>
 71<p><strong>A unified experience</strong> with the freedom to run anywhere, which allows developers to future-proof their work and eliminate vendor lock-in.</p>
 72</li>
 73</ul>
 74<h2 id="corner_in_cap_theorem"><a class="anchor" href="#corner_in_cap_theorem">¶</a>Corner in CAP theorem</h2>
 75<p>MongoDB’s position in the CAP theorem (Consistency, Availability, Partition Tolerance) depends on the database and driver configurations, and the type of disaster.</p>
 76<ul>
 77<li>With <strong>no partitions</strong>, the main focus is <strong>CA</strong>.</li>
 78<li>If there are **partitions **but the system is <strong>strongly connected</strong>, the main focus is <strong>AP</strong>: non-synchronized writes from the old primary are ignored.</li>
 79<li>If there are <strong>partitions</strong> but the system is <strong>not strongly connected</strong>, the main focus is <strong>CP</strong>: only read access is provided to avoid inconsistencies.
 80The general consensus seems to be that Mongo is <strong>CP</strong>.</li>
 81</ul>
 82<h2 id="download"><a class="anchor" href="#download">¶</a>Download</h2>
 83<p>We will be using the apt-based installation.</p>
 84<p>The Community version can be downloaded by anyone through <a href="https://www.mongodb.com/download-center/community">MongoDB Download Center</a>, where one can choose the version, Operating System and Package.MongoDB also seems to be <a href="https://packages.ubuntu.com/eoan/mongodb">available in Ubuntu’s PPAs</a>. </p>
 85<h2 id="installation"><a class="anchor" href="#installation">¶</a>Installation</h2>
 86<p>We will be using an Ubuntu-based system, with apt available. To install MongoDB, we open a terminal and run the following command:</p>
 87<pre><code>apt install mongodb
 88</code></pre>
 89<p>After confirming that we do indeed want to install the package, we should be able to run the following command to verify that the installation was successful:</p>
 90<pre><code>mongod --version
 91</code></pre>
 92<p>The output should be similar to the following: </p>
 93<pre><code>db version v4.0.16
 94git version: 2a5433168a53044cb6b4fa8083e4cfd7ba142221
 95OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
 96allocator: tcmalloc
 97modules: none
 98build environment:
 99	distmod: ubuntu1804
100	distarch: x86_64
101	target_arch: x86_64
102</code></pre>
103<h2 id="references"><a class="anchor" href="#references">¶</a>References</h2>
104<ul>
105<li><a href="https://www.mongodb.com/">MongoDB’s official site</a></li>
106<li><a href="https://www.mongodb.com/what-is-mongodb">What is MongoDB?</a></li>
107<li><a href="https://www.mongodb.com/mongodb-architecture">MongoDB Architecture</a></li>
108<li><a href="https://stackoverflow.com/q/11292215/4759433">Where does mongodb stand in the CAP theorem?</a></li>
109<li><a href="https://medium.com/@bikas.katwal10/mongodb-vs-cassandra-vs-rdbms-where-do-they-stand-in-the-cap-theorem-1bae779a7a15">What is the CAP Theorem? MongoDB vs Cassandra vs RDBMS, where do they stand in the CAP theorem?</a></li>
110<li><a href="https://www.quora.com/Why-doesnt-MongoDB-have-availability-in-the-CAP-theorem">Why doesn’t MongoDB have availability in the CAP theorem?</a></li>
111<li><a href="https://docs.mongodb.com/manual/installation/">Install MongoDB</a></li>
112</ul>
113</main>
114</body>
115</html>
116