MongoDB (Database service)

MongoDB is a cross-platform, document-oriented database.

For more information on using MongoDB, see MongoDB's own documentation.

Supported versions

  • 3.0
  • 3.2
  • 3.4
  • 3.6


Downgrades of MongoDB are not supported. MongoDB will update its own datafiles to a new version automatically but cannot downgrade them. If you want to experiment with a later version without committing to it use a non-master environment.


The format exposed in the $PLATFORM_RELATIONSHIPS environment variable:

    "username": "main",
    "scheme": "mongodb",
    "service": "mongodb",
    "ip": "",
    "hostname": "",
    "cluster": "rjify4yjcwxaa-master-7rqtwti",
    "host": "mongodb.internal",
    "rel": "mongodb",
    "path": "main",
    "query": {
        "is_master": true
    "password": "main",
    "type": "mongodb:3.6",
    "port": 27017

Usage example

In your .platform/services.yaml:

    type: mongodb:3.6
    disk: 1024

The minimum disk size for MongoDB is 512 (MB).

In your

    database: "mydatabase:mongodb"

For PHP, in your add:

        - mongodb

(Before PHP 7, use mongo instead.)

You can then use the service in a configuration file of your application with something like:



use Platformsh\ConfigReader\Config;
use MongoDB\Client;

// Create a new config object to ease reading the environment variables.
// You can alternatively use getenv() yourself.
$config = new Config();

// The 'database' relationship is generally the name of primary database of an application.
// It could be anything, though, as in the case here here where it's called "mongodb".
$credentials = $config->credentials('mongodb');

try {

    $server = sprintf('%s://%s:%s@%s:%d/%s',

    $client = new Client($server);
    $collection = $client->main->starwars;

    $result = $collection->insertOne([
        'name' => 'Rey',
        'occupation' => 'Jedi',

    $id = $result->getInsertedId();

    $document = $collection->findOne([
        '_id' => $id,

    printf("Found %s (%s)<br />\n", $document->name, $document->occupation);

} catch (\Exception $e) {
    print $e->getMessage();
const mongodb = require('mongodb');
const config = require("platformsh-config").config();

exports.usageExample = async function() {
    const credentials = config.credentials('mongodb');
    const MongoClient = mongodb.MongoClient;

    var client = await MongoClient.connect(config.formattedCredentials('mongodb', 'mongodb'));

    let db = client.db(credentials["path"]);

    let collection = db.collection("startrek");

    const documents = [
        {'name': 'James Kirk', 'rank': 'Admiral'},
        {'name': 'Jean-Luc Picard', 'rank': 'Captain'},
        {'name': 'Benjamin Sisko', 'rank': 'Prophet'},
        {'name': 'Katheryn Janeway', 'rank': 'Captain'},

    await collection.insert(documents, {w: 1});

    let result = await collection.find({rank:"Captain"}).toArray();

    let output = '';

    output += `<table>

    Object.keys(result).forEach((key) => {
        output += `<tr><td>${result[key].name}</td><td>${result[key].rank}</td></tr>\n`;

    output += `</tbody>\n</table>\n`;

    // Clean up after ourselves.

    return output;
from pymongo import MongoClient
from platformshconfig import Config

def usage_example():

    # Create a new Config object to ease reading the environment variables.
    # You can alternatively use os.environ yourself.
    config = Config()

    # The 'database' relationship is generally the name of primary SQL database of an application.
    # It could be anything, though, as in the case here here where it's called "mongodb".
    credentials = config.credentials('mongodb')

        formatted = config.formatted_credentials('mongodb', 'pymongo')

        server = '{0}://{1}:{2}@{3}'.format(

        client = MongoClient(server)

        collection = client.main.starwars

        post = {
            "name": "Rey",
            "occupation": "Jedi"

        post_id = collection.insert_one(post).inserted_id

        document = collection.find_one(
            {"_id": post_id}

        return 'Found {0} ({1})<br />'.format(document['name'], document['occupation'])

    except Exception as e:
        return e

Exporting data

The most straightforward way to export data from a MongoDB database is to open an SSH tunnel to it and simply export the data directly using MongoDB's tools.

First, open an SSH tunnel with the CLI:

platform tunnel:open

That will open an SSH tunnel to all services on your current environment, and produce output something like the following:

SSH tunnel opened on port 30000 to relationship: mongodb
SSH tunnel opened on port 30001 to relationship: redis

The port may vary in your case. You will also need to obtain the user, password, and database name from the relationships array, as above.

Then, simply connect to that port locally using mongodump (or your favorite MongoDB tools) to export all data in that server:

mongodump --port 30000 -u main -p main --authenticationDatabase main --db main

(If necessary, vary the -u, -p, --authenticationDatabase and --db flags.)

As with any other shell command it can be piped to another command to compress the output or redirect it to a specific file.

For further references please see the official mongodump documentation.


To upgrade to 3.6 from a version earlier than 3.4, you must successively upgrade major releases until you have upgraded to 3.4. For example, if you are running a 3.0 image, you must upgrade first to 3.2 and then upgrade to 3.4 before you can upgrade to 3.6.

For more details on upgrading and how to handle potential application backward compatibility issues, please see Release Notes for MongoDB.