Connect using Compass for Public Cloud Databases for MongoDB

Knowledge Base

Connect using Compass for Public Cloud Databases for MongoDB


Icons/System/eye-open Created with Sketch. 83 Views 06.01.2025 MongoDB

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 one of the world's most famous Open Source (SSPL) management tool for MongoDB: MongoDB Compass.

Requirements

  • A Public Cloud project in your OVHcloud account
  • A MongoDB database running on your OVHcloud Public Cloud Databases (this guide can help you to meet this requirement)
  • Configure your MongoDB instance to accept incoming connections
  • A MongoDB Compass stable version installed and public network connectivity (Internet). This guide was made in MongoDB Compass version 1.30.1.

Concept

A MongoDB instance can be managed through multiple ways. One of the easiest, yet powerful, is to use a Command Line Interface (CLI), as shown in our guide: Connect to MongoDB with CLI or by using programming languages, such as PHP or Python.

Another way is to interact directly using a management tool for MongoDB: MongoDB Compass.

In order to do so, we will need to install MongoDB Compass, then configure our Public Cloud Databases for MongoDB instances to accept incoming connections, and finally configure MongoDB.

Instructions

Installation

Pleese follow the official documentation to install MongoDB Compass.

We are now ready to learn how to connect to our MongoDB instance.

Connect with MongoDB Compass

In MongoDB Compass fill in the connection field with the Service URI:

New connection

Now you are now interact with your Public Cloud Databases for MongoDB:

Connected

Insert and Query Data

You can use the mongoshell, integrated in Compass, to create your first database and collection. Below is a script that creates the database company and collection customer and inserts 100 random documents.

Load Data into MongoDB

To load 100 documents into a collection called customer with random data, use the following mongosh script:

use company;

// Function to generate random data
function getRandomData() {
    const firstNames = ["John", "Jane", "Mary", "Michael", "Sarah", "Robert", "Linda", "James", "Patricia", "David"];
    const lastNames = ["Smith", "Johnson", "Williams", "Jones", "Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor"];
    const domains = ["example.com", "email.com", "mail.com", "test.com", "demo.com"];

    function getRandomItem(array) {
        return array[Math.floor(Math.random() * array.length)];
    }

    return {
        firstName: getRandomItem(firstNames),
        lastName: getRandomItem(lastNames),
        email: `${getRandomItem(firstNames).toLowerCase()}.${getRandomItem(lastNames).toLowerCase()}@${getRandomItem(domains)}`,
        age: Math.floor(Math.random() * 60) + 18,
        createdAt: new Date()
    };
}

// Insert 100 random documents into the customer collection
const bulk = db.customer.initializeUnorderedBulkOp();
for (let i = 0; i < 100; i++) {
    bulk.insert(getRandomData());
}
bulk.execute();

print("100 random documents inserted into the 'customer' collection.");

Compass Shell

Query Data with the Aggregation Framework

The MongoDB aggregation pipeline below uses the MongoDB Aggregation Framework to group customers by age and count each occurence. You can use the mongoshell to execute:

db.customer.aggregate([
    {
        $group: {
            _id: "$age",
            count: { $sum: 1 }
        }
    },
    {
        $sort: { _id: 1 }
    }
]);

You can also use the UI with Compass to execute the aggregation pipeline.

Compass Aggregation

Go further

Explore the documentation to view all the features and how to interact with your data.

MongoDB capabilities

Configuring vRack for Public Cloud

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

Join our community of users.

We want your feedback!

We would love to help answer questions and appreciate any feedback you may have.

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. Are you on Discord? Connect to our channel at https://discord.gg/ovhcloud and interact directly with the team that builds our databases service!