MongoClient

interface MongoClient

A Mongo client is used to access an App Service's Data Source directly without Sync support.

This API corresponds to the Atlas App Service "MongoDB API". Please consult the MongoDB API Reference for a detailed description of methods and arguments.

Serialization to and from EJSON is performed with KBSON that supports the Kotlin Serialization framework and handles serialization to and from classes marked with Serializable. Serialization can be customized by customizing the EJson-serializer passed to the various MongoClient, MongoDatabase and MongoCollection-factory methods.

Object references (links) are serialized solely by their primary keys, so to serialize the MongoDB API requests and responses to and from realm objects (RealmObject, EmbeddedRealmObject and AsymmetricRealmObject) the serialization framework must be configured with special serializers for those. This can be done with

val user = app.currentUser
val client = user.mongoClient(
"serviceName",
EJson(
serializersModule = realmSerializerModule(
setOf(
MongoDBCollectionDataType1::class,
MongoDBCollectionDataType2::class
)
)
)

NOTE Since the MongoDB API responses only includes primary key information for links, serialization of responses into realm objects (RealmObject, EmbeddedRealmObject and AsymmetricRealmObject) will create instances of the target objects with only the primary key property set. All other properties from the realm objects will have the default values specified in the class definition.

NOTE The EJSON serializer requires to opt-in to the experimental ExperimentalKBsonSerializerApi.

Properties

Link copied to clipboard
abstract val serviceName: String

The name of the data source that the MongoClient is connecting to.

Functions

Link copied to clipboard
@ExperimentalKBsonSerializerApi
inline fun <T : BaseRealmObject> MongoClient.collection(eJson: EJson? = null): MongoCollection<T>

Get a MongoCollection that exposes methods to retrieve and update data from the remote collection of objects of schema type T.

Link copied to clipboard
@ExperimentalKBsonSerializerApi
abstract fun database(databaseName: String, eJson: EJson? = null): MongoDatabase

Get a MongoDatabase object to access data from the remote collections of the data source.