= 1024) leftSidebarOpen = false; if(window.innerWidth >= 1280) rightSidebarOpen = false"<br>:class="{ 'dark': $store.theme.dark, 'left-sidebar-closed': !leftSidebarDesktop, 'right-sidebar-closed': !rightSidebarDesktop, 'overflow-hidden': leftSidebarOpen || rightSidebarOpen, 'sidebars-closed': !leftSidebarDesktop && !rightSidebarDesktop }">
Conway's Game of Life Just Using Mongo Shell
Skip to content
Download Free
0" x-text="$store.bookmarks.count" class="nav-bookmarks-count" x-cloak>
Bookmarks
0">
No bookmarks yet.
Bookmarks are saved locally on this browser.
= 1280 ? rightSidebarDesktop = !rightSidebarDesktop : rightSidebarOpen = true"<br>class="nav-icon-btn"<br>:class="{ 'is-active': !(window.innerWidth >= 1280 ? rightSidebarDesktop : rightSidebarOpen) }"<br>aria-label="Toggle sidebar">
= 1280 ? !rightSidebarDesktop : !rightSidebarOpen" d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4V3z" fill="currentColor" stroke="none" />
VisuaLeaf
Download
Navigation
if (window.innerWidth
Search articles…
Preferences
Layout
Save Sidebar view
Remember if sidebars are open or closed across pages.
Save Sidebar layout preference
Enable Bookmarks
Save your favourite articles locally in this browser.
Enable Local Bookmarks
let scroll = window.scrollY;<br>let docHeight = document.documentElement.scrollHeight - window.innerHeight;<br>this.progress = docHeight > 0 ? Math.max(0, Math.min(1, scroll / docHeight)) : 0;<br>this.ticking = false;<br>});<br>}"<br>@scroll.window.passive="update()"<br>@resize.window.passive="update()"<br>x-init="update()"<br>class="fixed top-0 left-0 w-full h-[3px] z-[100] pointer-events-none">
Conway's Game of Life Using The Mongo Shell
It really surprises me the number of people who don't know how the mongo shell works. Most of the people I see seem to think it's just db.find()s or db.aggregate()s with some print statements, and that's it. The mongo shell is interesting because it's fundamentally different from your regular SQL editor, where you can't declare a variable or write a loop. But the mongo shell is so much more than just MongoDB functions. It's a full JavaScript runtime built on Node.js, with variables, functions, closures, classes, and so much more. Your database is just an object sitting inside it.
In this article, I want to show the perspective of not just using the MongoDB functions in the mongo shell, but actually using the full potential of the shell, with examples like Conway's Game of Life and some custom chart diagrams.
Now first, the mongo shell was designed with JS in mind (prints, for loops, if statements, all of that), so using its full potential to manage your MongoDB database is crucial. If you wanted to migrate data, or if you wanted to create a script that edits certain types of data with conditions, you can do all of that with the mongo shell. I've been seeing a LOT of people trying to make their own shell with just db.find(), db.aggregate(), and print, but that's not a shell… it's literally just an empty shell of what the mongo shell actually is (pun intended).
Charts in your console, no library needed
Now, let's get on with something fun the mongo shell can do before we talk about Conway's Game of Life. The shell can print stuff to the console, so you can actually make a lot of cool visualizations right in your mongo shell. Here I made a shell script that literally visualizes data, so you don't need a charting library to see what's in your database. It's simple, but it's fun and it does the job:
function bar(coll, field) {<br>const rows = db.getCollection(coll).aggregate([<br>{ $group: { _id: "$" + field, count: { $sum: 1 } } },<br>{ $sort: { count: -1 } }<br>]).toArray();<br>const max = Math.max(...rows.map(r => r.count));<br>rows.forEach(r => {<br>const len = Math.round(r.count / max * 40);<br>print(String(r._id).padEnd(12) + " │" + "█".repeat(len) + " " + r.count);<br>});
bar("orders", "status");
Visualizing Bar Charts using The Mongo ShellAs you can see, with this simple script we were able to visualize bar charts just like that. And notice what's actually happening here: this is the shell being a language, not a command line. The aggregate() call is normal MongoDB. $group counts how many documents share each value of the field, and $sort puts the biggest first. But everything after that is plain JavaScript. We store the results in a variable, use Math.max to find the largest count, then loop over the rows and print() a bar for each one, scaled to 40 characters wide with "█".repeat(). A variable, a spread, a loop, string methods. None of that exists in a SQL prompt. Here it's just Tuesday.
Okay, now Conway's Game of Life
Now, by utilizing prints the same way we did for the chart generation, we can actually simulate the famous Conway's Game of Life JUST using mongosh, which is crazy. I'm not going to delve too much into Conway's Game of Life itself, so here are the quick rules.
It's a grid of cells that are either alive or dead, and every generation, each cell counts its 8 neighbors:
A live cell...