[SOLVED] Grapher Aggregation Lookup on Array of Foreign Keys

Hi,

I’m struggling to figure out how to use Grapher to perform an aggregated find on an array of foreign keys.

For example, I can run this MongoDB query and get the result I expect:

db.getCollection('items').aggregate([
    {
        $lookup: {
            from: "optionGroups",
            localField: "options",
            foreignField: "_id",
            as: "optionChoices"
        }
    }
])

This returns this result:

The ‘optionChoices’ field at the bottom corresponds to the ‘options’ field that is highlighted. The actual document just has the ‘options’ property which is an Array of Strings containing the IDs of documents in another collection.

Can I perform this same query in Grapher? If so, how do I do that? I’m struggling to understand how I would do it.

I’ve tried to keep this initial explanation short but I can find provide more context on the schemas, links and queries if required.

I’ve managed to solve this with help from @diaconutheodor on the GitHub page.

My mistake was that although I had created the links like so:

ItemCollection.addLinks({
  optionChoices: {
    type: 'many',
    field: 'options',
    collection: OptionGroupsCollection
  }
});
OptionGroupsCollection.addLinks({
  item: {
    collection: ItemCollection,
    inversedBy: 'optionChoices'
  }
});

I was not referencing the link name in my createQuery. I was referencing the ‘options’ field - I had assumed the data would be within ‘options’ when returned.

2 Likes