DEV Community

Cover image for Use MongoDB from CLI (Bite-size article)
koshirok096
koshirok096

Posted on

Use MongoDB from CLI (Bite-size article)

Intro

In this article, I will write about how to access your MongoDB account (Atlas) from CLI.

Typically, to manage the things on MongoDB account, I believe that many people would use a web browser, an editor extension (like VSCODE), or MongoDB Compass (GUI).

Most people around me typically manage things using the methods mentioned above, but I recently discovered the option of using CLI. I believe this method is quite common as well, but I personally only found it recently!

I'm sure that this CLI way make your coding journey more fun.
In this article, as an introduction, I will explain the process of accessing your MongoDB account only (so this article is quite short).

In this article, as an introduction, I will explain the process of accessing your MongoDB account only (so this article is quite short).

🌿 For this article, the prerequisite is that you already have a MongoDB account (Atlas). To better understand the process of the article, I recommend that you read the article after creating a MongoDB account and a cluster. πŸ’»

πŸ™‡β€β™‚οΈ Sorry for Window lovers, but this article is for Mac user... 🍎

Install mongodb-atlas-cli

To check the contents of a MongoDB Atlas cluster with the CLI, you can use the MongoDB Atlas CLI (mongocli). First thing first, let's look at the execution procedure.

We will use it by Homebrew, so please get it in advance if you don't have it in your computer yet.

Run below codes on Terminal:

brew install mongodb-atlas-cli
Enter fullscreen mode Exit fullscreen mode

And then, let's check the version of it.

mongocli --version 
Enter fullscreen mode Exit fullscreen mode

If you can see the version, that means the installation was successfully done!

Tip: 'mongodb-atlas-cli' or 'mongocli'?

It’s a minor detail, but have you noticed that despite installing it with the 'mongodb-atlas-cli' command, subsequent operations (like version checks) are actually named 'mongocli'?

This is because the MongoDB Atlas CLI is provided under the 'mongocli' command, and that's the official command name.

So, it's standard practice to use 'mongocli' instead of 'mongodb-atlas-cli' (I initially found it a bit confusing).

Image description

Verification and Check

At this point, the CLI configuration is just in, so your account needs to be tied to it. So, run the following code.

mongocli auth login
Enter fullscreen mode Exit fullscreen mode

Image description

If it works properly, then web browser will open and you will see a screen where you can enter the Verification Code. Simply enter the code displayed in the CLI.


For one particular aspect here, I'd like to apologize.

While I was setting up my account like above, I recall performing some operations related to Atlas, such as configuring user settings and authentication, in addition to the login procedure mentioned in this guide. However, I didn't take any notes, so I've forgotten the specific steps I took πŸ˜…

If you encounter any discrepancies with the procedures outlined here, please adjust your approach accordingly.


Anyways, after the process finished successfully, you will see the message 'Successfully logged in as "your email address"' in the Terminal. And you will be asked which of your MongoDB projects you want to use as default.

Image description

Now you can see your projects in MongoDB from the CLI screen. And that means you have successfully connected to your account.

Outro

Image description

After connecting to CLI, you will be able to do various operations, such as checking and editing the details of the information in your MongoDB account.

I wanted to write more about this, but I didn't have much time today, sorry!

I may write more about this topic in the future - when I come up with ideas, so please stay tuned.

Thank you for reading, happy coding!

Top comments (0)