Data API and Linked Objects

I’ve got some apps that use Atlas App Services for mobile, but I’m attempting to create a static website to view a bit of data. Basically, there is an Event object with an EventPlan object. I’m able to connect and query for these objects and display their data.

But then I run into issues. I’ve got 2 levels of linked object lists.

My simplified model looks like this:

Event: {
  _id: String
  plan: EventPlan
}

EventPlan: {
  _id: String
  blocks: EventBlock[]
}

EventBlock: {
  _id: String
  items: EventItem[]
}

The EventPlan object has an array of EventBlock objects.
The EventBlock objects have arrays of EventItem objects.
It’s those arrays of objects that are causing me grief.

I can see the _id for each EventBlock in the EventPlan.blocks array, but the only way I have found to retrieve all of the objects in the list are to hit the /action/findOne endpoint, but that seems like an extremely inefficient way to do it and is basically unusable with all of the back and forth.

I’ve experimented with trying to create a filter string with the _ids to get all of them from /action/find endpoint to no avail, and honestly, that seems like I’m doing it wrong.

Is there a clean way to get all of the linked objects with the Data API endpoints as they currently work?

Hey, just going to post this here (probably for when I google it in the future :joy:), but I was able to solve this after a very helpful call with @Sumedha_Mehta1.

The answer is in calling /action/aggregate

Instead of including a filter, you build a simple pipeline with all of the primary key ids from the parent object.

For instance, in my situation I had an EventPlan object with the blocks list of EventBlock objects. Realm/App Services create an array of Primary Keys.

So I simply created a string with the keys like this:

  let eventBlockIds = eventPlan.eventBlocks;
  let pipeline = [{
    $match: {
      "_id": {
        $in: ebs,  
      },
    },
  }]

When passing that into pipeline like is mentioned in the DataAPI docs, it worked as expected.
:tada::tada::tada:

1 Like

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