cult-of-coders / grapher

Grapher: Meteor Collection Joins + Reactive GraphQL like queries
https://atmospherejs.com/cultofcoders/grapher
MIT License
275 stars 53 forks source link

Array of Foreign Keys & Aggregation #445

Closed LordA98 closed 3 years ago

LordA98 commented 3 years ago

Hi,

This isn't an issue, but 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:

image

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.

theodorDiaconu commented 3 years ago

It's a relation 1:M or M:M Item -> Options. It's clear you have options, so make a link called optionChoices that has field: "options" and is of type many, comming from Item to Options.

LordA98 commented 3 years ago

Thank you! Your comment made me realise my mistake.

I had created the links but was not referencing the link name in my query, I assumed the result would come through in the 'options' field.

Thanks for the quick response.