Skip to content

MongoDB connection from applications

Connect Java, PHP, Python, and Node.js applications to MongoDB on Stack Harbor PaaS — driver code, connection strings, replica-set URIs, and TLS.

This page covers the application-side connection details for MongoDB on Stack Harbor PaaS. The MongoDB wire protocol is the same across drivers — they all accept a connection-string URI of the form mongodb://... — so the per-language code below is mostly the URI plus the driver’s constructor.

For the host options (CNAME, private IP, public IP), see Database connection strings first.

Connection-string format

Single-node:

mongodb://{user}:{password}@node{node_id}-{environment_name}.{domain}:27017/{dbname}

Replica set (list all members, name the set):

mongodb://{user}:{password}@node{a}-{env}.{domain}:27017,node{b}-{env}.{domain}:27017,node{c}-{env}.{domain}:27017/{dbname}?replicaSet=rs0

Common query-string options:

  • replicaSet=rs0 — required for a replica-set connection.
  • retryWrites=true — driver retries idempotent writes once on transient failure. Default in current drivers.
  • w=majority — write concern; block until a majority of members has acknowledged.
  • readPreference=primary (or secondary, nearest, etc.) — read routing.
  • tls=true — enable TLS in transit (the Encryption in Transit add-on must be installed on the database).

Java (mongodb-driver-sync)

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;

String uri = "mongodb://{user}:{password}@node{node_id}-{environment_name}.{domain}:27017/{dbname}";
MongoClient client = MongoClients.create(uri);

For a replica-set URI, the same MongoClients.create(uri) call works — the driver parses replicaSet=rs0 from the query string and handles topology discovery.

PHP

The MongoDB PHP extension (and the mongodb/mongodb library on top):

$client = new MongoDB\Client('mongodb://{user}:{password}@node{node_id}-{environment_name}.{domain}:27017/{dbname}');
$db = $client->selectDatabase('{dbname}');
$collection = $db->selectCollection('{collection}');

For a replica set, pass the full multi-member URI to the Client constructor.

Python (pymongo)

from pymongo import MongoClient

client = MongoClient('mongodb://{user}:{password}@node{node_id}-{environment_name}.{domain}:27017/{dbname}')
db = client['{dbname}']
collection = db['{collection}']

For an asyncio-based application, use motor (the async wrapper around pymongo):

from motor.motor_asyncio import AsyncIOMotorClient

client = AsyncIOMotorClient('mongodb://{user}:{password}@...')
db = client['{dbname}']

Node.js (mongodb)

import { MongoClient } from 'mongodb';

const uri = 'mongodb://{user}:{password}@node{node_id}-{environment_name}.{domain}:27017/{dbname}';
const client = new MongoClient(uri);
await client.connect();

const db = client.db('{dbname}');
const collection = db.collection('{collection}');

For long-running applications, create one MongoClient per process and reuse it — the client maintains an internal connection pool. Do not call new MongoClient per request.

Notes

  • No localhost. Application and database are on separate containers. Use CNAME, private IP, or public IP.
  • Default port. MongoDB listens on 27017 by default.
  • TLS. Install the Encryption in Transit add-on on the database, then append tls=true to the connection string. The driver does the rest.
  • Connection pooling. Every MongoDB driver maintains an internal pool. The application-level rule is: create the client once at startup, reuse it. Do not open a new client per request or per query.

See also