Realm array of arrays relations

Hi,

i created this in realm as my schema :
{ "title": "cake", "properties": { "_id": { "bsonType": "objectId" }, "ingredients": { "bsonType": "array", "items": { "bsonType": "array", "items": { "bsonType": ["string", "objectId"] } } } } }

but i dont know how to setup the relationships for the array in array objectId and cant ind anything related to this. Is this even possible?

If you are looking to manually define the schema in the UI - there is a relationship tab you can select to create a relationship in your already defined document. From your example above though it looks like you are trying to create an embedded document, this does not require the relationship builder tab since the relationship is implied with embedding. It should be noted that embeded documenrs do not have an _id field - https://docs.mongodb.com/realm/sdk/node/data-types/embedded-objects/

If you already have an object schema in a Realm SDK defined, sometimes it is easier to use developer mode to define your schema, particularly when it comes to relationships -

1 Like

I use GraphQL API to fetch the data. I wanna use 1 query to get all ingredients in an array. So i tried to create an array “ingredients” in “cake” and another array in “ingredients” to connect the specific amount of the ingredient and the ingredient.
But now i decided to change the array in “ingredients”-array to an Object, now i have the array “ingredients” which contains objects with “amount” and “ingredientID”. The ingredientID should give the details about the ingredient, e.g. name.

My query should be something like this: query { cake { _id name ingredients { amount ingredient { name } } } }

In the relationships tab i selected “ingredients [object]” as Field and “_id - objectId” as the property in the ingredients collection. This didnt work because “The types of “Field” and “Property” must match.”. So i edited the relationships myself and added:
"ingredients.[].ingredient": { "foreign_key": "_id", "ref": "#/relationship/mongodb-atlas/db/ingredients", "is_list": false }

But now i cannot perform a query like above. Im not able to get the name of the ingredient, it just returns the id as a string.

If you use an embedded document you do not need to define a relationship, you will also get all embedded documents in a single query result when querying on the parent document.

Relationships are really useful for defining a Realm Object Model which is leveraged with Realm Sync because the schema on the client-side Realm SDK and the server-side MongoDB Atlas must match for Sync to work. And relationships are integral to building a client side mobile app in the object-oriented paradigm. Relationships are stored as string values of the _id of the child document in the actual parent Atlas document.

If you’re just using GraphQL you can define the schema as you see fit as long as it matches Atlas. But if your documents are normalized and you want to pull in related documents you’ll need to run two queries, one to get the parent object which will return the list of document ids in other collections, and then a second query to fetch the child documents you need. You may also leverage a custom resolver to pull automatically these documents automatically since it allows you to configure a server-side function to be run on a GraphQL request and then return the documents in the format you desire.

We also support the ability to traverse relationships in GraphQL defined in the schema the same way standard GraphQL would allow you to, see here:

I hope this helps

2 Likes