Learn how to connect to a MongoDB database instance with a Command Line Interface (CLI).
Managed Databases (also called Cloud Databases) allow you to focus on building and deploying cloud applications while OVHcloud manages the database infrastructure and maintenance in operational conditions.
Requirements
- a Public Cloud project in your OVHcloud account
- access to the OVHcloud Control Panel
- a MongoDB database running on your OVHcloud Cloud Databases (this guide can help you to meet this requirement)
- configure your MongoDB instance to accept incoming connections
Concept
A MongoDB instance can be managed in 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, to connect and control the MongoDB service remotely. It's a Client-Server interaction.
Instructions
Install MongoDB Shell on your client
You first need to install 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, etc. The only requirements are to reach the public network (Internet) and have sufficient rights to install it.
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 these steps after selecting a Windows, MacOS, or Linux operating system.
We will now follow official MongoDB documentation to perform our first connection.
In your CLI, run the following command:
The result should look like this:
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. Don't forget to modify the username, password, and hostname.
Once connected correctly, you should see something similar to:
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 none exist)
- db.myCollection.insertOne( ... ) will insert data in your collection
In our example, it will look like this:
Go further
For more information and tutorials, please see our other MongoDB and Cloud Databases support guides or explore the guides for other OVHcloud products and services.
Visit the GitHub examples repository to find out how to connect to your database with several languages.
If you need training or technical assistance to implement our solutions, contact your sales representative or click on this link to get a quote and ask our Professional Services experts for a custom analysis of your project.