Skip to content

Instantly share code, notes, and snippets.

View nol166's full-sized avatar
🍎

John McCambridge nol166

🍎
  • MongoDB
  • Austin, TX
View GitHub Profile
@nol166
nol166 / instruqt_log.sh
Created February 22, 2024 18:03
easier to read instruqt logs
#!/bin/bash
# Check if a track ID is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <track-id>"
exit 1
fi
TRACK_ID="$1"
bold_if_special() {
@nol166
nol166 / gitlb.sh
Created July 24, 2023 16:29
git - list most recently checked out branches
gitlb='for k in `git branch | perl -pe s/^..//`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1`\\t$k; done | sort -r'
@nol166
nol166 / .mongoshrc.js
Created May 19, 2023 12:00
my .mongoshrc file
// default prompt
const short = false;
// shorthand for show collections and dbs
const sc = () => db.getCollectionNames();
const sd = () =>
db
.getMongo()
.getDBs()
.databases.map((db) => db.name);
@nol166
nol166 / gitlb.sh
Created May 19, 2023 11:57
git - show rcently checked out branches
gitlb='for k in `git branch | perl -pe s/^..//`; do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1`\\t$k; done | sort -r'
@nol166
nol166 / getExecutionStats.js
Last active March 27, 2023 18:18
Get query execution stats in mongosh
// Add to ~/.mongoshrc.js
// getExectionTime(<query>, <collection>, [hint])
// Usage example: getExecutionTime({ "name": "John" }, "users", { "name": 1 })
const getExecutionTime = (query, coll, hint = null) => {
hint ? console.log("Using hint: ", hint) : null;
query ? console.log("Using query: ", query) : null
currentDB = db.getName();
if (currentDB === "admin" || currentDB === "test") {
throw new Error("Cannot run getExecutionTime() in admin or test databases")
@nol166
nol166 / m201_notes.md
Created November 7, 2022 14:45
m201 Notes

M201 Notes

What are Indexes?

Think of indexes as a set of key value pairs, where each key is the value of the field that we indexed on, and the value (of the key) is the document itself. You can have multiple indexes on the same collection. Like an index in the back of a book, index keys are stored in order. Because of this, we don't need to look at every single index entry. MongoDB uses a B-Tree to store this information. Without an index, a new insertion will mean that a new comparison is needed, but with an index, not every insertion means that a new comparison is needed. As a result, querying is faster with an index. O^ log n vs O^ n.

Indexes are not without overhead however. Whenever a document is changed or deleted, the B-Tree needs to be updated. You shouldn't not have so many indexes that it slows down the CRUD operations. You should have enough indexes to make the queries fast, no more and no less.

@nol166
nol166 / alignment.sh
Last active August 3, 2021 22:00
Alignment script for the fullstack-online repo
# bash
# This script is used align the module with the ground repository.
# TODO --dry-run: add do not actually create any files
# prompt the user for the module name and ground repository
module_name=$1
ground_repository=$2