MongoDB is a document-oriented database, suited to schemas that change often or to nested data. On Webgune, MongoDB 7 exists only as a project database, a service in your project with its volume. There is no managed MongoDB.
| Value | |
|---|---|
| Version | MongoDB 7 |
| Port | 27017 |
| User | kxuser, unless you chose otherwise at creation. It is an administrator account, declared in the admin database |
| Storage | 5 GB volume |
| Resources | at most 1 vCPU and 1 GB of memory, taken from your plan |
| Instances | one only, no replica set |
Create a MongoDB database
- In the project, New service, then Deploy MongoDB.
- Fill in the Database name. The User is
kxuserif the field is left empty, the Password is generated if the field is left empty (at least 8 characters otherwise). - Click Create.
The service's Database tab shows the internal host, the port, the user, the masked password and the full connection string.
Connection string
MONGODB_URI=mongodb://kxuser:MOT_DE_PASSE@ma-db.kx-app-1a2b3c4d.svc.cluster.local:27017/ma-db?authSource=admin
?authSource=admin is required: the account is declared in the admin database, not in your database. Without this parameter, the connection fails with Authentication failed.
The internal address answers only the services of the same project (Network and isolation).
Mongoose (Node.js)
import mongoose from "mongoose";
await mongoose.connect(process.env.MONGODB_URI!, { maxPoolSize: 10 });
const PostSchema = new mongoose.Schema({
title: { type: String, required: true },
content: String,
createdAt: { type: Date, default: Date.now, index: true },
});
export const Post = mongoose.model("Post", PostSchema);
process.on("SIGTERM", async () => {
await mongoose.disconnect();
process.exit(0);
});
PyMongo (Python)
import os
from pymongo import MongoClient
client = MongoClient(os.environ["MONGODB_URI"], maxPoolSize=10)
db = client.get_default_database() # the database named in the URI
posts = db.posts.find({"published": True})
Indexes
MongoDB indexes only _id by default. A query on a non-indexed field scans the whole collection, which shows from a few thousand documents and uses the memory limited to 1 GB.
await Post.collection.createIndex({ createdAt: -1 });
In production, turn off Mongoose's automatic index creation (autoIndex: false) and create the indexes once, at deployment.
Access from your machine
Turn on Public access in the Database tab. The panel shows a Public host and a Public port, different from 27017.
mongosh "mongodb://kxuser:MOT_DE_PASSE@HOTE_PUBLIC:PORT_PUBLIC/ma-db?authSource=admin"
MongoDB Compass accepts the same string. The public port is open to the whole Internet, with no address list: only the password protects the database. Turn the access off after the intervention.
Queries from the panel
The console in the Database tab runs the command with mongosh --eval and stops it after 30 seconds. It suggests db.stats() by default. Examples:
db.getCollectionNames()
db.posts.find({ published: true }).limit(5).toArray()
db.posts.countDocuments()
Backups
mongodump --uri "mongodb://kxuser:MOT_DE_PASSE@HOTE_PUBLIC:PORT_PUBLIC/ma-db?authSource=admin" --out ./sauvegarde
mongorestore --uri "mongodb://kxuser:MOT_DE_PASSE@HOTE_PUBLIC:PORT_PUBLIC/?authSource=admin" ./sauvegarde
The service's Backups tab also schedules volume snapshots (Backups and restore).
Limits to know
- No replica set: multi-document transactions and change streams, which need one, are not available.
- A single instance. During a restart or a server failure, the database is unavailable.
- The Move to a managed database button does not exist for MongoDB.
Common errors
| Message | Cause | What to do |
|---|---|---|
Authentication failed | authSource=admin missing, or wrong password | copy the string from the Database tab again |
Transaction numbers are only allowed on a replica set member | transaction or causal session requested | remove the transaction, or write related documents in a single operation |
getaddrinfo ENOTFOUND | internal address used outside the project | turn on Public access |
| Stop with code 137 | the database exceeds 1 GB of memory | add the missing indexes, reduce unbounded queries |
Updated on