<?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: Fang He</title>
    <description>The latest articles on DEV Community by Fang He (@cloudbillrescue).</description>
    <link>https://dev.to/cloudbillrescue</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4278587%2F87fcc95e-f80d-475f-accd-f035cde6bc8d.png</url>
      <title>DEV Community: Fang He</title>
      <link>https://dev.to/cloudbillrescue</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cloudbillrescue"/>
    <language>en</language>
    <item>
      <title>Why Sorting MongoDB Slow Queries by Count Misleads You (and a Zero-Dep Go CLI Replacing mtools)</title>
      <dc:creator>Fang He</dc:creator>
      <pubDate>Wed, 02 Dec 2026 13:19:38 +0000</pubDate>
      <link>https://dev.to/cloudbillrescue/why-sorting-mongodb-slow-queries-by-count-misleads-you-and-a-zero-dep-go-cli-replacing-mtools-1g92</link>
      <guid>https://dev.to/cloudbillrescue/why-sorting-mongodb-slow-queries-by-count-misleads-you-and-a-zero-dep-go-cli-replacing-mtools-1g92</guid>
      <description>&lt;p&gt;When a production MongoDB cluster hits 90%+ CPU, the most common first mistake is sorting slow query logs by &lt;strong&gt;entry count&lt;/strong&gt; and tuning whatever shows up most often.&lt;/p&gt;

&lt;p&gt;In real production audits, the noisiest query is rarely the real killer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In one 15,921-entry slow log I audited, only &lt;strong&gt;92 &lt;code&gt;COLLSCAN&lt;/code&gt; entries (0.6%)&lt;/strong&gt; drove the majority of database work — a single query scanned ~2.5M documents and read ~18 GB from disk.&lt;/li&gt;
&lt;li&gt;In another 10,817-entry log, one query shape scanned &lt;strong&gt;45.27 billion documents&lt;/strong&gt; across 2,711 executions while sharing the cluster with write-heavy collections where blindly adding indexes would hurt replication lag.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Tooling Gap on MongoDB 4.4–8.0+
&lt;/h2&gt;

&lt;p&gt;Historically, DBAs reached for &lt;code&gt;mtools&lt;/code&gt; (&lt;code&gt;mloginfo&lt;/code&gt;, &lt;code&gt;mlogfilter&lt;/code&gt;). Unfortunately, &lt;code&gt;mtools&lt;/code&gt; stops at legacy MongoDB 4.2 plaintext logs and throws format errors on modern structured JSON logs (Logv2).&lt;/p&gt;

&lt;p&gt;Writing ad-hoc &lt;code&gt;jq&lt;/code&gt; pipelines works for 10 MB files, but breaks down fast on multi-GB &lt;code&gt;.gz&lt;/code&gt; production logs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;jq&lt;/code&gt; does not collapse identical queries that have different literal user/order IDs into one canonical &lt;strong&gt;Query Shape&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Loading multi-GB logs into Node/Python scripts frequently OOMs.&lt;/li&gt;
&lt;li&gt;Pasting raw production logs into an LLM hits context limits and leaks customer PII.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Introducing &lt;code&gt;mongodigest&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;I open-sourced &lt;strong&gt;&lt;a href="https://github.com/cloudbillrescue/mongodigest" rel="noopener noreferrer"&gt;mongodigest&lt;/a&gt;&lt;/strong&gt; — a zero-dependency, zero-network, single-binary Go CLI for offline forensic triage of MongoDB 4.4–8.0+ JSON slow logs (&lt;code&gt;mongod.log&lt;/code&gt;, &lt;code&gt;.gz&lt;/code&gt;, &lt;code&gt;stdin&lt;/code&gt;) and &lt;code&gt;mongoexport&lt;/code&gt; &lt;code&gt;system.profile&lt;/code&gt; exports.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Design Choices
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;0 External Dependencies &amp;amp; 0 Network Calls:&lt;/strong&gt; &lt;code&gt;go.mod&lt;/code&gt; has zero third-party packages and &lt;code&gt;net/http&lt;/code&gt; is not even imported. Safe to run air-gapped on a laptop or jump box.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GB-Scale Streaming Without OOM:&lt;/strong&gt; Uses a custom 1 MiB reader instead of Go's 64 KB &lt;code&gt;bufio.Scanner&lt;/code&gt; limit so queries with 5,000-element &lt;code&gt;$in&lt;/code&gt; lists never abort, and auto-detects &lt;code&gt;.gz&lt;/code&gt; streams via magic bytes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Guaranteed Literal Redaction &amp;amp; &lt;code&gt;getMore&lt;/code&gt; Attribution:&lt;/strong&gt; Replaces all scalar literals with &lt;code&gt;?&lt;/code&gt; / &lt;code&gt;[?]&lt;/code&gt;, normalizes key order deterministically, and unwraps &lt;code&gt;getMore&lt;/code&gt; &lt;code&gt;originatingCommand&lt;/code&gt; back to the originating query shape.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Risk-Graded DBA Triage (&lt;code&gt;[SAFE]&lt;/code&gt; / &lt;code&gt;[NEEDS VALIDATION]&lt;/code&gt; / &lt;code&gt;[HIGH RISK]&lt;/code&gt;):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Ranks shapes by &lt;code&gt;cpuNanos&lt;/code&gt; (or falls back smoothly to &lt;code&gt;durationMillis&lt;/code&gt;), &lt;code&gt;docsExamined&lt;/code&gt;, &lt;code&gt;keysExamined&lt;/code&gt;, and &lt;code&gt;storage.data.bytesRead&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Derives candidate &lt;strong&gt;Equality → Sort → Range (ESR)&lt;/strong&gt; compound indexes — and &lt;strong&gt;automatically elevates &lt;code&gt;COLLSCAN&lt;/code&gt; index suggestions to &lt;code&gt;[HIGH RISK]&lt;/code&gt;&lt;/strong&gt; if the same collection shows heavy write traffic in the log.&lt;/li&gt;
&lt;li&gt;Catches &lt;code&gt;IXSCAN&lt;/code&gt; illusions (large &lt;code&gt;$in&lt;/code&gt; + in-memory &lt;code&gt;SORT&lt;/code&gt;) and high-return bulk reads (&lt;code&gt;&amp;gt;= 50%&lt;/code&gt; return ratio) where adding indexes won't cut I/O.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hourly-Normalized &lt;code&gt;diff&lt;/code&gt; &amp;amp; &lt;code&gt;--export-prompt&lt;/code&gt;:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Compare &lt;code&gt;before.log.gz&lt;/code&gt; and &lt;code&gt;after.log.gz&lt;/code&gt; normalized per hour (&lt;code&gt;/hour&lt;/code&gt;) even when capture windows differ.&lt;/li&gt;
&lt;li&gt;Export a ~3–4 KB 100% redacted prompt pack (&lt;code&gt;--export-prompt -&lt;/code&gt;) to paste safely into Claude/ChatGPT.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Try It in 1 Second (No Prod Logs Needed)
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;mongodigest&lt;/code&gt; ships with realistic synthetic MongoDB 7.0 slow logs built in:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go install github.com/cloudbillrescue/mongodigest/cmd/mongodigest@latest

&lt;span class="c"&gt;# Terminal triage demo&lt;/span&gt;
mongodigest demo

&lt;span class="c"&gt;# Generate an offline single-file HTML report you can share with your team/CTO&lt;/span&gt;
mongodigest demo --out demo-report.html

&lt;span class="c"&gt;# Compare before vs after optimization rates (/hour)&lt;/span&gt;
mongodigest demo diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3oxbvq16vnl2e1m0jclq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3oxbvq16vnl2e1m0jclq.png" alt="mongodigest CLI terminal triage output showing top MongoDB slow query shapes and risk findings" width="800" height="766"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fquuyzx41bh1lqu2u2ch5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fquuyzx41bh1lqu2u2ch5.png" alt="mongodigest self-contained offline HTML diagnostic report for MongoDB slow logs" width="800" height="933"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Running on Your Own Logs
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Self-hosted or MongoDB Atlas .gz logs&lt;/span&gt;
mongodigest analyze mongod.log.gz --out report.html

&lt;span class="c"&gt;# Export a redacted 4 KB prompt for AI review&lt;/span&gt;
mongodigest analyze mongod.log.gz --export-prompt - &lt;span class="o"&gt;|&lt;/span&gt; pbcopy

&lt;span class="c"&gt;# Managed MongoDB (system.profile JSONL)&lt;/span&gt;
mongoexport --uri=&lt;span class="s2"&gt;"$MONGO_URI"&lt;/span&gt; -c system.profile --out profile.jsonl
mongodigest analyze profile.jsonl --out profile-report.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Source &amp;amp; pre-built binaries (Linux/macOS/Windows): &lt;strong&gt;&lt;a href="https://github.com/cloudbillrescue/mongodigest" rel="noopener noreferrer"&gt;https://github.com/cloudbillrescue/mongodigest&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you run into weird Atlas, &lt;code&gt;mongos&lt;/code&gt;, or &lt;code&gt;system.profile&lt;/code&gt; log edge cases, feel free to open an issue (redacted sample lines welcome!).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(Built from real-world production database rescue work at &lt;a href="https://cloudbillrescue.com/database-performance-rescue/" rel="noopener noreferrer"&gt;CloudBillRescue&lt;/a&gt;. Independent open-source tool, not affiliated with MongoDB, Inc.)&lt;/em&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>go</category>
      <category>devops</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
