Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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 -sSL https://raw.githubusercontent.com/mongodb-js/mongosh/refs/heads/main/download_latest.sh | sh
```

## CLI Usage

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

set -o errexit

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

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). Download mongosh manually."
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; download manually."
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 -j "$file" '*/mongosh'
;;
tgz)
echo "Downloading & extracting from $url"

curl -fsSL "$url" | tar -xzf - \
--transform "s/.*\///" \
--wildcards "**/mongosh"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd probably want mongosh_crypt_v1.so to be extracted next to mongosh

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should maybe know this, but: why so? What breaks if it’s not there? (It worked for me without.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automatic encryption for Queryable Encryption & CSFLE don't – not everybody cares about those, but I don't think we'd like to ship "partially broken" versions of mongosh if we don't need to

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, the script now downloads both the executable and the shared library.


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

echo "Success! 'mongosh' is now saved in this directory."