Learn how to connect to a MongoDB® database instance with a Command Line Interface (CLI).
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
Please note that MongoDB® Shell is also natively integrated into the MongoDB® community software package.
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 the 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
Visit the GitHub examples repository to find out how to connect to your database with several languages.
For more information and tutorials, please see our other Managed Databases & Analytics or Platform as a Service guides. You can also explore the guides for other OVHcloud products and services.
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.
OVHcloud Managed Databases and Analytics:
- Grafana® is a registered trademark of Grafana Labs and is used with the permission of Grafana Labs. OVH SAS and its subsidiaries are not affiliated with or endorsed by Grafana Labs.
- Kafka® is a registered trademark of The Apache Software Foundation and has been licensed for use by OVHcloud, who has no affiliation with and is not endorsed by The Apache Software Foundation.
- MongoDB® is a registered trademark of MongoDB, Inc.
- MySQL® is a registered trademark of Oracle and/or its affiliates.
- PostgreSQL® is a registered trademark of the PostgreSQL Community Association of Canada.