How To Retrieve _ID For Use In Partition Key

Hi everyone, I’m new to MongoDB. I’m trying to use Realm Sync with a simple Xamarin Forms app. When the app initially starts, it asks the user for firstname, lastname, email, etc. I generate a GUID that I use as the _id. I can successfully write to the database, and I see the document show up in Atlas. My question, which has to be super simple but I’m struggling with, is: when the user opens the app up the 2nd time, how do I pull that _id to send in as the partitionkey? I use a partitionkey of “user=_id”. In order to get the partitionkey (the _id), I need to query the database first to pull it out, but I can’t query the database without the partitionkey (or maybe I can and I’m doing something wrong?). I am just starting out, so I have development mode enabled and I’m logging in anonymously. Do I need to save that _id locally somewhere and pull it from there? Or is there a way to query the local realm database without specifying the partitionkey (since there would only be 1 document in there)? Sorry for such a stupid question. Any help is appreciated.

Hi @Ja_B,

This is already done by Realm at the first login/signup: when the user is created in the Realm Portal for your app, an Id is assigned to it, and recovered at every subsequent login. Thus, after a login, this is always available as the Id of the App.CurrentUser (assuming there is a single user within the app, obviously), before you have accessed the DB.

That is typically the user.id that you can then refer within other parts of Realm (Functions, Permissions, etc.)

Thanks, Paolo! It looks like that’s the way to go if I have each user create an account in MongoDB. I was trying to figure out if there was a way to login anonymously and return back the entire collection (without knowing/passing in the partition key) or, alternatively, open the realm database locally (which would only have the 1 user in it) and retrieve the _id that way. Thanks again!