Solving complex queries with 3 objects

I’m struggling to determine the best way to perform a query including 3 different objects I currently store in my Realm database. I can’t decide if I need to re-structure the objects or maybe I just can’t figure out an efficient query.

I have 3 objects. Recipe, Coffee and Brew.

A Brew has both associated Recipe and an associated Coffee

Recipe and Coffee each have an inverse relationship property called brew that is of type LinkingObjects<Brew>. This inverse relationship provides the list of Brews associated with a given Recipe or Brew.

I need effectively query What Recipe is most often used with a given Coffee` in a brew.

Any tips on tackling this problem?

Hello Connor_Ricks,

Welcome to MongoDB!

So something I would recommend, is that to prevent this from getting complicated is to create a Sorted Linked List.

The exact algorithm I would try first is called a Merge Sort.

So I would merge the two linked lists with the Merge Sort, and take that new list and Merge Sort that in parallel with the Brews.

Then you have the coffee and recipe sorted and merged by most to least, and associate it to the appropriate brew.

There may be less complicated ways of doing this, but this method is what sticks out the most.

Otherwise, you can let MongoDB do the work for you. :heart_eyes: with indexes and aggregations. You can then build a Realm Function to do the indexes. Or you can build a GraphQL function to sort it, your call.

But if you must have Swift be programmed to do it, then the above is my recommendation to ensure performance and smoothness.

But if you’re just populating this information to say a dashboard, I would go with an Index and funnel the data to a dashboard using our Data API for the outputs. Which would probably be the easiest, but it is in “Preview” is our Data API. For this you’d need HTTP services.

Basically just have this query your Atlas Cluster, do the indexes and so on, and then have it generate the output to the end user.

But to me, personally speaking I would let MongoDB do the work for you. We’ve made it a point to make these sort of things possible. It honestly just comes down to what you feel is best for your use case, because we have tutorials across our blogs for using GraphQL for this, which is a part of your Atlas subscription. (Free) as well as Realm Functions (Free), the Data API (Free) of course hard coding internally into your application via Swift and having it sort the Realm Objects accordingly is an option.

MongoDB University has some great classes for teaching MongoDB (Atlas) indexes, aggregations, etc.

Let me know how I can help with this,



Thanks for the detailed write-up Brock! I appreciate the thought you put into it. I ended up going with the approach of leaning on Realm/Mongo more heavily and performing some aggregations. It’s not necessarily the most performant, but it’s not a super common area of my app. I think the ease/readability of implementation is worth the extra processing power.

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