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

Objective

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).

Requirements

Concept

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.

Instructions

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 : https://docs.mongodb.com/mongodb-shell/install/#std-label-mdb-shell-install

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
1.5.1

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>@mongodb-e49d02ee-o2626ab53.database.cloud.ovh.net/admin?replicaSet=replicaset"

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>@mongodb-e49d02ee-o2626ab53.database.cloud.ovh.net/admin?replicaSet=replicaset&appName=mongosh+1.5.1
Using MongoDB:      4.4.6
Using Mongosh Beta: 1.5.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

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
admin
replicaset [direct: primary]> use myNewDatabase
switched to db myNewDatabase
replicaset [direct: primary]> db
myNewDatabase
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: https://discord.gg/ovhcloud. Ask questions, provide feedback and interact directly with the team that builds our databases services.

Join our community of users on https://community.ovh.com/en/.


Cette documentation vous a-t-elle été utile ?

N’hésitez pas à nous proposer des suggestions d’amélioration afin de faire évoluer cette documentation.

Images, contenu, structure… N’hésitez pas à nous dire pourquoi afin de la faire évoluer ensemble !

Vos demandes d’assistance ne seront pas traitées par ce formulaire. Pour cela, utilisez le formulaire "Créer un ticket" .

Merci beaucoup pour votre aide ! Vos retours seront étudiés au plus vite par nos équipes..


Ces guides pourraient également vous intéresser...

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

Conformément à la Directive 2006/112/CE modifiée, à partir du 01/01/2015, les prix TTC sont susceptibles de varier selon le pays de résidence du client
(par défaut les prix TTC affichés incluent la TVA française en vigueur).