Query Mongo DB remote via Kotlin

I’m struggling to process a query via mongo remote access using Kotlin.

Referencing this section, which seems to be outdated?

The snippet offered looks like this:

val queryFilter = Document("_partition", "Store 42")
val findTask = mongoCollection.find(queryFilter).iterator()
findTask.getAsync { task ->
    if (task.isSuccess) {
        val results = task.get()
        Log.v("EXAMPLE", "successfully found all plants for Store 42:")
        while (results.hasNext()) {
            Log.v("EXAMPLE", results.next().toString())
        }
    } else {
        Log.e("EXAMPLE", "failed to find documents with: ${task.error}")
    }
}

But when I try to call .getAsync on the iterator, I run into trouble. There is no .getAsync. There’s a getResult. But I can’t even figure out how to get that to do anything.

My code at the moment (just trying to test by pulling a doc with a specific _id) looks like this:

val client = realmApp.currentUser()?.getMongoClient("mongodb-atlas")
val database = client?.getDatabase("funky_radish_db")
val collection = database?.getCollection("Recipe")

val queryFilter = Document("_id", "617ee621638d87c7faf71c9f")
val findTask = collection?.find(queryFilter)?.iterator()

Log.d("API", "task: $findTask")

And the output is

task: com.google.android.gms.tasks.zzu@475b07a

How do I work with these task objects? Was .getAsync deprecated?
And, if I can get that part working, how do I adjust the query filter to work for an _id_in query?
something like what I was able to accomplish in Swift using this:
let queryFilter: Document = ["_id": ["$in": recipes]]

@Ryan_Goodwin: May I know why you are accessing DB directly instead of using Realm for accessing documents?

That’s correct @Mohit_Sharma

Isn’t that clear from the code though?

val client = realmApp.currentUser()?.getMongoClient("mongodb-atlas")
val database = client?.getDatabase("funky_radish_db")
val collection = database?.getCollection("Recipe")

Or am I missing something?

@Ryan_Goodwin: You are trying to access MongoDB directly, instead of using Realm like mentioned in this section.

I am recommending you this way as it gives you more flexibility in manipulating data. You can refer to this on how to get started with Realm

1 Like

Correct @Mohit_Sharma, I’d prefer to access MongoDb directly.
Are you suggesting there is no way to do this? I am able to do it using Swift, and of course Javascript on a web frontend, so I was assuming that it should be possible via Kotlin/android.

In this specific case, I just want to read data. I don’t need the flexibility of a synchronized Realm, so the overhead of partitioning is overkill.

@Ryan_Goodwin: Sorry for the late response, I missed your thread notification. Let me check on this and get back to you, I don’t have expertise around this.

@Ryan_Goodwin: Based on the discussion with my colleague, we think that Data API is the perfect solution for your use case.

Awesome, thanks @Mohit_Sharma I’ll look into that.

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