How to specify specific database to sync from Realm Android Kotlin

By default when we initialize our app with realmSync, it uses the default database name (android/ios app name). But in the cloud we might have several databases and if someone wants to change the database how would he do that?

We know only Realm.init(context) for android

Realm only has the concept of a TableName. Each table name has a mapping to a single MongoDB Namespace (Database Name and Collection Name). The “Default Database” name is really only used when you are defining new TableNames in your SDK because we need to figure out which “database” to put it in. If you want some of your tables to be in databaseA and some to be in databaseB, then you need to define that in the Realm UI. In the “Schemas” section you can create a schema and specify the TableName->(DatabaseName, CollectionName) explicitly

Hi, I may have mixed up what you are asking. Are you referring to the mapping in the cloud of the MongoDB database name / collection name, or the file that the local realm database is stored to on the device?

G’Day, @Neeraj_42037,

Welcome to MongoDB Community forums :wave:

This code snippet is initializing the Realm SDK in your android application. This is not defining any database yet. You can further set up the app configuration using API.

There is mostly 1 database per Realm Application on the cloud and multiple public/private realms as part of that and is segregated based on the partition strategy you define. You can have multiple databases but they are tricky to use when you have partition sync, as your partition key should be the same in all the databases.

Could you clarify more what you are trying to do and where you are stuck? Are you using Sync or only the local database? If you are using sync, do you have development mode on?

I look forward to your response.

Cheers, :performing_arts:
Henna

Yes! I have 2 databases running on mongodb cloud as shown in fig


The first database is Employee and the 2nd database is RChat.

Imaging if i have created 2 different application named App1 and App2

So i just want my first application “App1” to use Emplyoee database and the 2nd application “App2” to use RChat.

How can i write code for specifying the database for android application [Java SDK]?

@Neeraj_42037: This configuration is done/managed by the Sync tab under your Realm App.

1 Like

This option is only enabled for development mode. However i have defined that too but still app is automatically creating the database with app name and using it.


Its still using “CodeCamp” database

@Neeraj_42037: I am not sure what happened. I have just validated the same use case and it’s working fine on my side.

Usecase: Validate if the data is getting pushed to the right database or not. Also after the DB name is changed from the sync tab.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.