Can I use value besides `var _id` as primaryKey() for MongoDB Synch?

I have an iOS app that was created before the acquisition of Realm by Mongo. I am in the process of updating the code to work with Realm Synch.

Reading through the documentation it states that I should add the following to the class declarations I wish to synch:

    // Some of the sample code videos uses this as well, which appears to be the same as above.
    // I am assuming both accomplish the same.
    // @objc dynamic var _id: ObjectId = ObjectId.generate()

    @objc dynamic var _id = NSUUID().uuidString
    override static func primaryKey() -> String? {
        return "_id"
    }

All of the class declarations I have already use:

    @objc dynamic var uuid = NSUUID().uuidString
    override static func primaryKey() -> String? {
        return "uuid"
    }

Here is an example

Many of my classes look like this:

final class UserInfo: Object {
    @objc dynamic var someVar = ""
    @objc dynamic var anotherVar = ""

    @objc dynamic var uuid = NSUUID().uuidString
    override static func primaryKey() -> String? {
        return "uuid"
    }
}

Can I use what I already have using the var uuid instead of var _id. I have about 40 classes and need to convert and would prefer not to do that, if possible.

Thx in advance.

Welcome to the forums!

To work with sync, objects MUST use _id as their primary key

Primary Key _id Required

To work with Realm Sync, your data model must have a primary key field called _id . _id can be of type string , int , or objectId .

So you will want to change your models now, before moving to a sync environment.

Some interesting information here ObjectId with this note

If an inserted document omits the _id field (value), the MongoDB driver automatically generates an ObjectId for the _id field.

and then the other part of the question

Well, kind of. The point is to generate unique values, which they both do. However, Realm Documents are BSON so the ObjectId may better align with that format. IMO, I would be using ObjectId going forward.

This is from the docs and including it for completeness

ObjectId and UUID (Universal Unique Identifier) both provide unique values that can be used as identifiers for objects. ObjectId is a MongoDB-specific 12-byte unique value. UUID is a standardized 16-byte unique value. Both types are indexable and can be used as primary keys.

ObjectId has additional features like being able to retrieve the timestamp directly from the object.