For AI agents: the complete documentation index is available at https://docs.us.ovhcloud.com/en/llms.txt, the full documentation bundle is available at https://docs.us.ovhcloud.com/en/llms-full.txt, and this page is available as Markdown at https://docs.us.ovhcloud.com/en/public-cloud/databases/mongodb-connect-mongodb-compass.md.
  • English

MongoDB® - Connect with MongoDB Compass

View as Markdown

Learn how to connect to a MongoDB® database instance with one of the world's most famous Open Source (SSPL) management tools for MongoDB®: MongoDB Compass.

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.

 

Requirements

Info

This guide was made in MongoDB® Compass version 1.30.1.

 

Concept

A MongoDB® instance can be managed in multiple ways. One of the easiest and most 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.

To do so, we will need to install MongoDB® Compass, configure our Cloud Databases for MongoDB® instances to accept incoming connections, and finally configure MongoDB®.

 

Instructions

Installation

Please 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 URI field with your service URI and click Connect:

DBaaS mongo connectcompass - step 1

Now you can interact with your Cloud Databases for MongoDB®:

DBaaS mongo connectcompass - step 2

 

Insert and Query Data

You can use the mongoshell, integrated into 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.");

Public cloud databases 07 images

 

Query Data with the Aggregation Framework

The MongoDB® aggregation pipeline below uses the MongoDB® Aggregation Framework to group customers by age and count each occurrence. 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.

Public cloud databases 07 images - step 2

 

Go further

Visit the GitHub examples repository to find out how to connect to your database in several languages.

Browse all the Databases guides.

Browse the rest of the OVHcloud US documentation for guides on our other products and services.

Learn more about Managed Databases on the OVHcloud US website.

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.