Realm React - specifying a database

In Node.js we can access a specific database in the following manner

mongoose.db(<db name>).collection(<collection name>)

How do i do this in realm react-native? Im trying to sync specific data in specific databases within the cluster in mongoDB Atlas, but it goes to a default database.

Also how do i configure 2 different databases. Let’s say i have “Purchases” database, and this database needs to update the “Clients” database and “Product” database. I know how to do this with mongoose but not with realm

Hi, when using developer mode to populate schemas, all tables are mapped to “defaultDBName.realmTableName”. If you want to map realm tables to specific database/collections, you can do so by setting this in the “Schemas” configuration in the UI.

I think this page is perhaps good to read through. If you have any other questions about it after reading through it, please let me know and I would be happy to assist.

Best,
Tyler

Thank you for replying, and forgive me for my lack of understanding, I’m new to realm.

So how would that look like in this schema?

export default class Product extends Realm.Object {
  _id!: Realm.BSON.ObjectId;
  title!: string;
  price!: number;
  descritpion!: string;

  static generate({title, price, description}) {
    return {
      _id: new Realm.BSON.ObjectId(),
      title,
      price,
      description,
    };
  }

  static schema: schemaType = {
    //DB name is shop
    //Collection name is products
    name: 'Product',
    primaryKey: '_id',
    properties: {
      _id: {type: 'objectId', default: () => new Realm.BSON.ObjectID()},
      title: 'string',
      price: 'double',
      description: 'string',
    },
  };
}

Hi, to change the mapping from Realm Table to MongoDB Namespace you need to configure the App Services application at realm.mongodb.com. See this screenshot:

I have created an app, it’s fully functional, with users, schemas, rules, etc.

My problem is, as you can see in the image below, I have a database named shop, with a collection named products, but when I sync, my objects go to a database named todo, but I want to specifically send them to shop.

I understand that this is because my sync settings in app services is set to development mode where you can specify a default database. But I want to pick specifically where my objects should be stored, and not default to a random database.

Hi, to pick specific db/collection to map your tables to you need do one of the following:

Configure it in the UI:

  1. Terminate Sync (assuming your application is not in production, this will make things easier)
  2. Copy the schemas in the todo database into the new database/collection you want them to map to in the UI you posted a screenshot of
  3. Remove the old schema definitions in the todo database
  4. Enable sync and connect your app. Everything should work (your client will reset due to terminating and re-enabling sync)

Use Developer Mode:

  1. Terminate Sync
  2. Remove the schemas in the UI screenshot you posted
  3. Enable sync and update the default database name to be “shop”

Let me know if that works for you.

Best,
Tyler

That’s very clear, i will do that now, thank you. But what happens, if I have two databases with the same collection and schema, how would it know which collection to sync to?

I will give you my real app scenario, given this is just a demo for me to try and work this out:

I’m building an app for hospital wards, each ward will have their own database, each database it’s own “patient” collection, among other collections with the exact same name. So you may have:

WardA (db)
     Patients (collection)
          <patient obj>
          <patient obj>
          <patient obj>
          ...

WardB (db)
     Patients (collection)
          <patient obj>
          <patient obj>
          <patient obj>
          ...

I was planning to specify the database to sync to with state, based on the user that is logged in to the app. Example with mongoose:

const user = WardA

const collection = mongoose.db(<user>).collection('patients').find( )

Is there a way to specify a database like that with react-native?

Are you trying to use sync or just the Remote Data Access built into the SDK’s? If you are trying to use sync that is not currently possible. Its worth mentioning that generally having databases per customer is an anti-pattern and you should try to use a single DB / Collection with a “ward_id” field or something like that.

So how would you structure this database, would you have all the patients, staff and all other info within the same database? so for example, the “patients” collection will hold all the patients for all the wards?

Also, say we have have 4 hospitals, each hospital has about 8 to 10 wards, each ward has around 25 patients, would you still recommend having everything in the same database.

The app is to be used in hospitals, I will need to sync data because you may not always have an internet connection, so I need the app to work online and off.

Hi, I would recommend having a single collection that holds the documents for all hospitals/users. I think this is a good documentation summary of how to think of multi-tenant architecture in MongoDB.