Configure different Atlas Database per environment

I have a single Atlas App Service that I want to have different environments (development, production, etc). I want to have each environment connect to a separate Atlas database. When I originally setup the app, I used the Atlas database named “TestDB”

I’ve read in How to Build CI/CD Pipelines for MongoDB Realm Apps Using GitHub Actions | MongoDB as well as other posts on how to use the environment.json to specify an environment variable (in my case: slm_database_name which is set to something like DevDB or ProdDB based on the environment) to specify the database. And I can use that variable in the server side functions that I write, e.g. in addTeamMember.js:

    const collection = context.services.get("mongodb-atlas").db(context.environment.values.slm_database_name).collection("User");

Great!

However, there are still places where the non-environment variable based database is referenced, and I can’t figure out how to set/change those. For example, if I do a “realm-cli pull”, there is an entire directory named data_sources/mongodb-atlas/TestDB, and within that I have files such as data_sources/mongodb-atlas/TestDB/Foo/relationships.json (where Foo has a relationship to another object) and the relationship is hard-coded with “TestDB” e.g.:

{
    "events": {
        "ref": "#/relationship/mongodb-atlas/TestDB/GameEvent",
        "source_key": "events",
        "foreign_key": "_id",
        "is_list": true
    }
}

or in sync/config.json has:

{
    ...
    "database_name": "TestDB",
    ...

Can someone tell me: once you setup the environment variable, what do you need to do in either the web UI or the json configuration files to use the environment variable that specifies the database? Or maybe I’m doing it wrong to begin with? If I use the environment name database_name instead of slm_database_name, will it magically work?

Thanks.

Using realm-cli for deployment, you can replace the hardcoded database name.
See my post here:

Thank you for that information. One followup question however, is the file really stored in a directory with the string %(%%environment.values.database)? Your example shows:

demoapp/data_sources/mongodb-atlas/%(%%environment.values.database)/Demo/rules.json

Which seems a little odd to me.

Thanks again!

Unfortunately, this doesn’t work. Atlas app services translates the variable upon setting and then hard-codes it. Changing the environment from Development to Testing or some other type doesn’t change the database thereafter. Also I’m getting errors when trying to enable sync such as:

recoverable event subscription error encountered: failed to configure namespaces for sync: failed to configure namespace ns=‘%%environment.values.slm_database_name.User’ for sync: error ensuring namespace exists: (InvalidNamespace) ‘.’ is an invalid character in a db name: %%environment.values.slm_database_name

Yes.

True, changing environment after deployment will not change your database. These values are for deployment with realm-cli. On the other hand, you can deploy multiple versions of the same app to the same project, each with any environment/database you want.

I’m using it like below, no problems with partition based sync (haven’t tried flexible).

relationships.json
{
  "previousContext": {
    "ref": "#/relationship/mongodb-atlas/%(%%environment.values.database)/ReportingContexts",
    "source_key": "previousContext",
    "foreign_key": "_id",
    "is_list": false
  }
}

rules.json
{
  "collection": "ReportingContexts",
  "database": "%(%%environment.values.database)",
  "roles": []
}