How do i have my own custom database collection names in Kotlin - Realm?

I want to create database collection name as “car_details” instead of “CarDetails” (which is model class name)
Is there any annotation which will internally map to my own custom name instead of model class name?

class CarDetails : RealmObject {
var make: String = “”
var model: String = “”
var miles: Int = 0
}

@Raviteja_Aketi: Sadly that’s not possible as of now. You can follow and upvote this issue for more updates.

Is it possible to create collections from console and insert data from mobile application (kotlin)?
There will not be any schema initialization from kotlin code. It has to get list of collections and insert data into the right collection

This way i can use my own collections names instead of model class name

Hi, @Raviteja_Aketi has the right idea here. I think this documentation page should do a good job of explaining it: https://www.mongodb.com/docs/atlas/app-services/sync/data-model/data-model-map/

The TLDR is that MongoDB has a Namespace (database.collection) and Realm has a TableName. The Schemas tab is where you can define the mapping between these two. By default we use the collection name to be the table name, but if you add a “title” to the json schema mapping then you can effectively map which table maps to which collection. Then you can have your car_details collection have a schema with the title “CarDetails” and I think that will get you what you want.

Best,
Tyler

But practically this is not possible with Kotlin SDK. Kotlin sdk always deals with data classes. I dont find any code to get list of collections and insert data into specific collection

I am not sure I understand your concerns / what you are trying to do then. Can you clarify:

  1. What model are you using?
  2. What is the corresponding schema in the schemas tab of the UI?
  3. What collection do you want this mapped to?

Hi @Tyler_Kaye

Here is the example. I need an example in Kotlin to map this model class to my custom collection names

Model class
class CarDetails : RealmObject {
var make: String = “”
var model: String = “”
var miles: Int = 0
}

I want to map this to car_details collection

Hi, like I linked above, if you want to map that class to a synced collection car_details, you can add a new schema in the “schemas” tab for the car_details collection and give the json schema a “title” field of CarDetails. Then you would have a Realm Class with the title CarDetails that maps to a MongoDB namespace DB_NAME.Car_Details.

Does this make sense?

Hi @Tyler_Kaye

There is no way to edit “title” field from MongoDB UI console and also there is no specific annotation in kotlin to edit this (Extend `@PersistedName` annotation to apply to classes · Issue #1138 · realm/realm-kotlin · GitHub).

Kotlin is default considering model class name as schema title but we don’t have provision to modify this

You can edit it right in that page if you want. Just note that updating the title is a “breaking” change since it is functionally “removing” the old table.

Please try the following:

  1. Create a new collection for car_details
  2. Paste a new JSON schema in
  3. Update the “title” to be CarDetails

Thanks,
Tyler

Here is the issue:

Extend @PersistedName annotation to apply to classes · Issue #1138 · realm/realm-kotlin · GitHub

In Kotlin, we can create like this. There is no way to put our custom collection names.
What ever you suggest that is not possible in Kotlin language.

class CarDetails : RealmObject {
var make: String = “”
var model: String = “”
var miles: Int = 0
}

Yes, I understand. Your model doesnt need to even know about what the collection name is though. It can just know that its table name is “CarDetails” and Device Sync knows the mapping it has to the “car_details” field through the “title” field in the JSON Schema.

Do you mind reading through https://www.mongodb.com/docs/atlas/app-services/sync/data-model/data-model-map/ and letting me know clearly what it is you are trying to accomplish and what you cannot do?

My model class name is CarDetails but i want to see it as a car_details as a collection name under mognogdb console.

I had gone through this but there is no way to define or modify schema “title” using kotlin code
https://www.mongodb.com/docs/atlas/app-services/sync/data-model/data-model-map/

It would be great if i get any kotlin examples for this mapping

Yes, this is not possible from the Kotlin side I do not think, but my point is that it should not matter if you setup the backend like I said above. You do not need to modify the “title” in the Kotlin code, you want to define it in the JSON Schema in the backend.

As per my understanding,

  1. Create schema at backend level
  2. Access remote database collection and sync data
    https://www.mongodb.com/docs/realm/sdk/java/examples/mongodb-remote-access/

But I want to maintain local database and sync data based on online/offline conditions.
I should maintain the same schema which is there at server level.
It would be great if i get any example kotlin code to understad this

Hi @Mohit_Sharma

I guess we have a provision to give custom schema titles using java annotations.
Can we create models in java language use it in kotlin realm? Is this feasible ?

@Raviteja_Aketi: No, that wouldn’t make sense as you are using Realm Kotlin SDK which doesn’t have implementation for this annotation but should be out very soon.

On a separate note, why do you want the collection name precisely like car_detail? Camel is normally used in MongoDB naming for collections as shown in the sample dataset if this is the primary concern.

I just gave an example. I need my custom collection name instead of model class name.
I just came across below example from Java SDK but don’t know whether same kotlin annotation works with kotlin SDK or not. Do you have any idea?

As mentioned earlier, No that wouldn’t work as Kotlin doesn’t have implementation “@RealmClass”.