In the example why type of User primary key is string and not objectId?

I’m wondering why in the example you decided to use string type as a User’s primary key and not ObjectId like it is in Task’s primary key. Does it has something to do with User Object's custom_data? So it can be only linked by string not objectId ? Can I use ObjectId instead? Does it has something to do with the partition key?

I have tried to change to ObjectId, and I get ERROR during the synchronization (SyncSession Start).

could not evaluate sync permissions with error: cannot compare to undefined (ProtocolErrorCode=206)


Edit: Question is specifically about a User’s primary key which is populated by the server. Below is asking about primary key’s in general.

Are you doing this?

class Task: Object {
    @objc dynamic var _id: ObjectId = ObjectId.generate()

so when the Task is instantiated, the _id is populated.

If you’re just doing this ObjectId() it’s only a new zero-initialized ObjectId

They use the later function in the section - not sure why.

The Id of MongoDB Realm users is a string. The fact that it’s generated from an ObjectId is an implementation detail and we do not make guarantees that this will be the case going forward. This is why the tutorial uses strings for the User object’s _id.

1 Like

If Id is a string, then why cannot .NET devs continue to generate string primary keys from Guids?

The Id is populated with the User’s Id generated by the server - it’s not something the SDK generates.

Typical of me … always getting it backwards :grinning:

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