Optional: persist notes in MongoDB
This chapter replaces the primary PostgreSQL attachment. It uses the same web routes,
Redis queue, worker, and object-storage integration. The app selects a notes collection with a unique id index, through its MongoDB driver.
This is a fresh dataset, not an automatic migration of your PostgreSQL notes.
1. Create the alternative database
Dashboard: in go create Database notes-mongodb, engine MongoDB, 1 GB storage.
Wait until running. CLI:
paas dbs create notes-mongodb --type mongodb --storage-gb 1 -o "$TUTORIAL_ORG" -p examples -e go
paas dbs info notes-mongodb -o "$TUTORIAL_ORG" -p examples -e go
2. Pause traffic and change both apps
Finish outstanding exports, then stop using the sample app while changing stores.
On the worker’s Overview, use − in Replicas to reach zero and confirm, or run paas scale 0 -a notes-worker -o "$TUTORIAL_ORG" -p examples -e go.
Dashboard: for both notes-web and notes-worker, open Config and detach only the
current primary database (DATABASE_URL). Attach notes-mongodb using DATABASE_URL,
then set DATABASE_ENGINE=mongodb. Keep Redis and bucket attachments.
CLI: list each app’s attachments and copy the ID for its primary database:
paas apps attachments notes-web -o "$TUTORIAL_ORG" -p examples -e go
paas apps attachments notes-worker -o "$TUTORIAL_ORG" -p examples -e go
Replace the two placeholders below with the attachment IDs, which differ per app and are not the database resource ID:
paas apps detach notes-web <web-primary-attachment-id> -o "$TUTORIAL_ORG" -p examples -e go
paas apps detach notes-worker <worker-primary-attachment-id> -o "$TUTORIAL_ORG" -p examples -e go
paas apps attach notes-web notes-mongodb -o "$TUTORIAL_ORG" -p examples -e go
paas apps attach notes-worker notes-mongodb -o "$TUTORIAL_ORG" -p examples -e go
paas config set DATABASE_ENGINE=mongodb -a notes-web -o "$TUTORIAL_ORG" -p examples -e go
paas config set DATABASE_ENGINE=mongodb -a notes-worker -o "$TUTORIAL_ORG" -p examples -e go
Do not keep a plain DATABASE_URL variable that could override the injected attachment.
Changing attachments/config rolls out the app automatically; wait until the web process is running.
See attachments and configuration precedence.
3. Initialize and verify
Dashboard: run /cnb/process/migrate in notes-web’s Console.
CLI:
paas run -a notes-web -o "$TUTORIAL_ORG" -p examples -e go -- /cnb/process/migrate
Expect Schema ready. Restore the worker to one replica using + on Overview or with
paas scale 1 -a notes-worker -o "$TUTORIAL_ORG" -p examples -e go. Create a new note identifying MongoDB, restart the web app,
and verify it remains. Generate a new export and confirm the new note appears in the JSON.
Previously generated export files are snapshots of the old store; they do not change.
To return to PostgreSQL, repeat the attachment change for notes-db and set
DATABASE_ENGINE=postgresql on both apps. Your earlier PostgreSQL notes remain there
until you explicitly delete that database. Each engine can be followed independently;
you do not have to finish the other optional chapter first.
Continue with MariaDB or MongoDB if you want to compare primary stores. Each is optional and starts a separate dataset. Then prepare the app for production or clean up the tutorial resources.