MongoDB - Se connecter avec CLI (EN)

Connect to your Public Cloud Databases for MongoDB using the Command Line Interface (CLI)

Last updated 27th July 2022


Public Cloud Databases allow you to focus on building and deploying cloud applications while OVHcloud takes care of the database infrastructure and maintenance in operational conditions.

This guide explains how to connect to a MongoDB database instance with a Command Line Interface (CLI).



A MongoDB instance can be managed through multiple ways. One of the easiest, yet powerful, is to use a Command Line Interface (CLI), also known as a Console or Terminal. To interact correctly with the MongoDB instance, we need to first install something called a MongoDB client, in order to connect and control the MongoDB service remotely. It's a Client-Server interaction.


Install MongoDB Shell on your client

Please note that MongoDB Shell is also natively integrated in the MongoDB community software package.

You first need to install a software to interact with your MongoDB instance remotely. This official software can be installed on various supports like your own computer, a VPS, a virtual machine... the only rule is to be able to reach the public network (Internet) and have sufficient rights to install it.

In order to do so and depending on your configuration, you may need to follow official MongoDB documentation to install MongoDB shell (referred to as mongosh).

Follow the steps here after selecting Windows, MacOS or Linux operation system :

We will now follow official MongoDB documentation to perform our first connection.

In your CLI, type mongosh --version. The result should look like this :

laptop$ mongosh --version

It means that mongosh is correctly installed and working properly. If you do not see something like this result, please go back to the previous step and reinstall MongoDB Shell.

Connect to your MongoDB instance

To perform a connection, simply type mongosh followed by the Service URI copied before :

laptop$mongosh "mongodb+srv://<username>:<password>"

Don't forget you need to modify the username, password and hostname.

Once connected correctly, you should see something similar to :

Current Mongosh Log ID: 6137881570829aa067222c24
Connecting to:      mongodb+srv://<credentials>
Using MongoDB:      4.4.6
Using Mongosh Beta: 1.5.1

For mongosh info see:

replicaset [direct: primary]>

Congratulations! You are now fully able to interact with your MongoDB instance!

MongoDB Shell usage

Once connected, you can manage your MongoDB instance with built-in MongoDB Shell methods.

Please follow the official MongoDB documentation.

To verify:

  • db : will indicate on which database you are
  • use myNewDatabase will switch to another database (will create one if not existing)
  • db.myCollection.insertOne( ... ) will insert data in your collection

In our example, it will look like this :

replicaset [direct: primary]> db
replicaset [direct: primary]> use myNewDatabase
switched to db myNewDatabase
replicaset [direct: primary]> db
replicaset [direct: primary]> db.myCollection.insertOne( { x: 1 } );
  acknowledged: true,
  insertedId: ObjectId("61378bc0fc37b3df64e48b18")
replicaset [direct: primary]> db.myCollection.find()
[ { _id: ObjectId("61378bc0fc37b3df64e48b18"), x: 1 } ]
replicaset [direct: primary]>

Go further

MongoDB capabilities

Configuring vRack for Public Cloud

Visit the Github examples repository to find how to connect to your database with several languages.

Visit our dedicated Discord channel: Ask questions, provide feedback and interact directly with the team that builds our databases services.

Join our community of users on

Did you find this guide useful?

Please feel free to give any suggestions in order to improve this documentation.

Whether your feedback is about images, content, or structure, please share it, so that we can improve it together.

Your support requests will not be processed via this form. To do this, please use the "Create a ticket" .

Thank you. Your feedback has been received.

These guides might also interest you...

OVHcloud Community

Accedez à votre espace communautaire. Posez des questions, recherchez des informations, publiez du contenu et interagissez avec d’autres membres d'OVHcloud Community.

Echanger sur OVHcloud Community