Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ Once downloaded, you will have to extract the binary and add it to your PATH
variable. For detailed instructions for each of our supported platforms, please visit
[installation documentation](https://www.mongodb.com/docs/mongodb-shell/install#mdb-shell-install).

You can also run `download_latest.sh` to download a `mongosh` binary. You can use
this script without cloning the repository thus:
```
curl -fsSL https://raw.githubusercontent.com/mongodb-js/mongosh/refs/heads/main/download_latest.sh | sh
```

## CLI Usage

<!-- AUTOMATICALLY_INSERT_CLI_USAGE -->
Expand Down
96 changes: 96 additions & 0 deletions download_latest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/sh

set -o errexit

MONGOSH_RELEASES_URL=https://downloads.mongodb.com/compass/mongosh.json

show_download_link() {
echo >&2 "Download mongosh manually from:"
echo >&2
printf >&2 "\t%s\n", 'https://www.mongodb.com/try/download/shell'
}

for tool in jq curl; do
which "$tool" >/dev/null || {
echo >&2 "This script requires '$tool'."
exit 1
}
done

os=$(uname -o | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)

case "$os" in
*linux)
ext=tgz
os=linux
;;
darwin)
ext=zip
;;
*)
echo >&2 "❌ This script does not support this OS ($os)."
show_download_link

exit 1
esac

# normalize $arch:
case "$arch" in
amd64|x64)
arch=x86_64
;;
aarch64)
arch=arm64
;;
*)
# Use uname’s reported architecture in the jq query.
esac

jq_query=$(cat <<EOF
.versions[0].downloads[] |
select(
.distro == "$os" and
.arch == "$arch" and
(has("sharedOpenssl") | not)
) |
.archive.url
EOF
)

url=$(curl -fsSL $MONGOSH_RELEASES_URL | jq -r "$jq_query")

if [ -z "$url" ]; then
echo >&2 "❓ No download found for $os on $arch."
show_download_link
exit 1
fi

case "$ext" in
zip)
file=$(mktemp)

echo "Downloading $url to $file …"
trap 'rm -f "$file"' EXIT

curl -fsSL "$url" > "$file"
echo "Downloaded $ext file; extracting mongosh …"

unzip -vj "$file" '*/bin/mongosh*'
;;
tgz)
echo "Downloading & extracting from $url …"

curl -fsSL "$url" | tar -xzf - \
--transform "s/.*\///" \
--wildcards "**/bin/mongosh*" \
| sed -E 's/^.*[/]//'

;;
*)
echo >&2 "Bad file extension: $ext"
show_download_link
exit 1
esac

echo "✅ Success! 'mongosh' and its crypto library are now saved in this directory."