Hello,
I would like to perform the following functionality but from Realm using Android/Kotlin as frontend.
Here it is:
db.getCollection('sessions').update(
{'_id' : ObjectId("5f5a3661d00ba84ba0247878")},
{
$set:
{
"rates.native.stars": NumberInt(3),
"rates.native.details": "abcde"
}
}
)
So I tried the following:
realm = Realm.getDefaultInstance()
realm.executeTransaction {
val item = it.where<session>().equalTo("_id", ObjectId("5f5a3661d00ba84ba0247878")).findFirst()
item?.rates?.native?.stars = 3
}
realm.close()
This didn’t work because the executeTransaction seems to work only on existing fields in the document and doesn’t insert/update new ones. In my case “stars” does not exist and I want to insert it.
Exactly as I am doing in the $set method. But I want something for Kotlin/Realm
Then I tried this one:
val user: User? = taskApp.currentUser()
val mongoClient : MongoClient? = user?.getMongoClient("myservicename")
val mongoDatabase : MongoDatabase? = mongoClient?.getDatabase("mydbname")
val mongoCollection : MongoCollection<Document>? = mongoDatabase?.getCollection("sessions")
val queryFilter : Document = Document("_id", ObjectId("5f5a3661d00ba84ba0247878"))
val updateDocument : Document = Document("rates.native.stars", 3)
.append("rates.native.details", "abcde")
mongoCollection?.updateOne(queryFilter, updateDocument)?.addOnCompleteListener {
if (it.isSuccessful) {
val count : Long = it.result.modifiedCount
if (count == 1L) {
Log.v("EXAMPLE", "successfully updated a document.")
} else {
Log.v("EXAMPLE", "did not update a document.")
}
} else {
Log.e("EXAMPLE", "failed to update a document with: ${it.exception}")
}
}
This one also didn’t work. I am getting that the document failed to update and that the update is not permitted. I did enable read and write from the Sync UI so that users can read and write but I am still getting this error.
Note that in the documentation, it shows:
mongoCollection?.updateOne(queryFilter, updateDocument)?.getAsync …
but this function is not loading in Android Studio. so I am use onCompleteListener instead.
Does anybody know how to convert the below function into a function that can be called from Android/kotlin using the Realm/Sync.
db.getCollection('sessions').update(
{'_id' : ObjectId("5f5a3661d00ba84ba0247878")},
{
$set:
{
"rates.native.stars": NumberInt(3),
"rates.native.details": "abcde"
}
}
)