How to declare array in schema in Realm Sync?

const WORKPLAN_SCHEMA = {
name: ‘workplans’,
properties: {
_id: “objectId”,
status: “int”,
zoneId: “???”, <—<< array
},
primaryKey: ‘_id’
};

We need to declare array and make relationship between this array with other tables.

------> like that
const WORKPLAN_SCHEMA = {
name: ‘workplans’,
properties: {
_id: “objectId”,
status: “int”,
zoneId: [
{
zoneId: “objectId” >>----> relation with zone table >>-----> { _id: “objectId”, zoneName: “test_1” }
},
{
zoneId: “objectId” >>----> relation with zone table >>-----> { _id: “objectId”, zoneName: “test_1” }
}
]
},
primaryKey: ‘_id’
};

Hi, we use JSON Schema so please see here for that: array — Understanding JSON Schema 2020-12 documentation

So you probably want something like:

const WORKPLAN_SCHEMA = {
    name: ‘workplans’,
    properties: {
        _id: {bsonType: “objectId”},
        status: {bsonType: “int”},
        zoneId: {bsonType: “array”, items: {bsonType: "objectId}},
}

And then you can add that this is a relationship in the UI in the relationships tab of the schema editor.

If, you are trying to use the JS SDK and define your schema model there (not sure as you didnt give much information about where the schema you give here is being defined), then please see this guide: https://www.mongodb.com/docs/realm/sdk/node/examples/define-a-realm-object-model/#define-a-to-many-relationship-property

That’s not how you define a relationship on the client - in the doc Tyler linked the example here shows how Person can have a list of Dogs

const Person = {
  name: "Person",
  properties: {
    name: "string",
    birthdate: "date",
    dogs: "Dog[]"
  }
};

const Dog = {
  name: "Dog",
  properties: {
    name: "string",
    age: "int",
    breed: "string?"
  }
};

If you’re having trouble setting up the schema you may want to just use developer mode, that way, you just need to setup the schema on the client side and it will be automatically replicated on the Server. -



of type ‘array’ has unknown object type ‘friendList’ i getting this error.

@ Ian_Ward

If you take a look at the error message you will see that your names are mismatched - you need to correct that.



@Ian_Ward

I can’t see the error message - can you paste it in here? Also, can you please share your Realm App UI URL (the URL of the Realm App in the browser) - so I can take a look at the logs ?

Are you wiping the local state on the device as you change the schema? You need to do that because you are making breaking changes. Also I think friendList needs to have a valid _id field if it is a standalone object

The Realm Server-side Cloud logs would tell us more as well

I dont know where we found the cloud logs. Can you send me the path.