Join

Join( source, detail )
Join( source, detail )
Join( source, detail )
Join( source, detail )
join( source, detail )
Join( source, detail )

Description

The Join function finds all index tuples from the source SetRef and uses the source's values to be retrieved from the detail index terms.

The run time of Join is dependent on the number of elements in the underlying set or page — it’s linear, or O(n). For very large sets or pages, executing Join might result in a query timeout error.

To work around this, you may specify a larger query timeout via the driver that you are using.

Parameters

Argument Type Definition and Requirements

source

SetRef

The source SetRef for the join operation.

detail

IndexRef or Lambda function

The IndexRef to join with the source SetRef, or the Lambda function which determines how to complete the join operation.

Returns

The SetRef for the join operation.

Examples

The index form is useful when the documents in the source_set match the terms in an index. Join returns documents from an Index (specified by detail) that match the terms from source.

client.Query(
  Paginate(
    Join(
      Match(
        Index("spellbooks_by_owner"),
        Ref(Collection("characters"), "181388642114077184")),
      Index("spells_by_spellbook"))));
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" }
  ]
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "join": {
              "match": { "index": "spellbooks_by_owner" },
              "terms": { "@ref": "classes/characters/181388642114077184" }
            },
            "with": { "index": "spells_by_spellbook" }
          }
        }'
HTTP/1.1 200 OK
{
  "resource": {
    "data": [
      { "@ref": "classes/spells/181388642046968320" },
      { "@ref": "classes/spells/181388642071085568" }
    ]
  }
}
result, _ := client.Query(
    f.Paginate(
        f.Join(
            f.MatchTerm(
                f.Index("spellbooks_by_owner"),
                f.RefCollection(f.Collection("characters"), "181388642114077184"),
            ),
            f.Index("spells_by_spellbook"),
        ),
    ),
)

fmt.Println(result)
map[data:[{181388642046968320 0xc4202478a0 <nil>} {181388642071085568 0xc420247ac0 <nil>}]]
client.query(
  Paginate(
      Join(
        Match(
          Index(Value("spellbooks_by_owner")),
          Ref(Collection("characters"), "181388642114077184")),
        Index(Value("spells_by_spellbook")))));
{
  data: [
    ref(id = "181388642046968320", collection = ref(id="spells", collection = ref(id = "collections"))),
    ref(id = "181388642071085568", collection = ref(id="spells", collection = ref(id = "collections")))
  ]
}
client.query(
  q.Paginate(
    q.Join(
      q.Match(
        q.Index('spellbooks_by_owner'),
        q.Ref(q.Collection('characters'), '181388642114077184')
      ),
      q.Index('spells_by_spellbook'),
    )
  )
)
.then((ret) => console.log(ret))
{ data:
   [ Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))),
     Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))) ] }
client.query(
  q.paginate(
    q.join(
      q.match(
        q.index("spellbooks_by_owner"),
        q.ref(q.collection("characters"), "181388642114077184")
      ),
      q.index("spells_by_spellbook")
    )
  ))
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" }
  ]
}
client.query(
  Paginate(
    Join(
      Match(
        Index("spellbooks_by_owner"),
        Ref(Collection("characters"), "181388642114077184")),
      Index("spells_by_spellbook"))))
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" }
  ]
}

The Lambda form requires the Lambda function to be pure. i.e. it may not make any reads or writes.

client.Query(
  Paginate(
    Join(
      Match(
        Index("spellbooks_by_owner"),
        Ref(Collection("characters"), "181388642114077184")),
      Lambda(
        "spellbook",
        Match(Index("spells_by_spellbook"), Var("spellbook"))))));
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" }
  ]
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "join": {
              "match": { "index": "spellbooks_by_owner" },
              "terms": { "@ref": "classes/characters/181388642114077184" }
            },
            "with": {
              "lambda": "spellbook",
              "expr": {
                "match": { "index": "spells_by_spellbook" },
                "terms": { "var": "spellbook" }
              }
            }
          }
        }'
HTTP/1.1 200 OK
{
  "resource": {
    "data": [
      { "@ref": "classes/spells/181388642046968320" },
      { "@ref": "classes/spells/181388642071085568" }
    ]
  }
}
result, _ := client.Query(
    f.Paginate(
        f.Join(
            f.MatchTerm(
                f.Index("spellbooks_by_owner"),
                f.RefCollection(f.Collection("characters"), "181388642114077184"),
            ),
            f.Lambda(
                "spellbook",
                f.MatchTerm(
                    f.Index("spells_by_spellbook"),
                    f.Var("spellbook"),
                ),
            ),
        ),
    ),
)

fmt.Println(result)
map[data:[{181388642046968320 0xc42029a8e0 <nil>} {181388642071085568 0xc42029ab00 <nil>}]]
client.query(
  Paginate(
      Join(
        Match(
          Index(Value("spellbooks_by_owner")),
          Ref(Collection("characters"), "181388642114077184")),
        Lambda(
          Value("spellbook"),
          Match(
            Index(Value("spells_by_spellbook")),
            Var("spellbook"))))));
{
  "data": [
    ref(id = "181388642046968320", collection = ref(id="spells", collection = ref(id = "collections"))),
    ref(id = "181388642071085568", collection = ref(id="spells", collection = ref(id = "collections")))
  ]
}
client.query(
  q.Paginate(
    q.Join(
      q.Match(
        q.Index('spellbooks_by_owner'),
        q.Ref(q.Collection('characters'), '181388642114077184'),
      ),
      q.Lambda(
        'spellbook',
        q.Match(q.Index('spells_by_spellbook'), q.Var('spellbook')),
      )
    )
  )
)
.then((ret) => console.log(ret))
{ data:
   [ Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))),
     Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))) ] }
client.query(
  q.paginate(
    q.join(
      q.match(
        q.index("spellbooks_by_owner"),
        q.ref(q.collection("characters"), "181388642114077184")
      ),
      q.lambda_(
        "spellbook",
        q.match(
          q.index("spells_by_spellbook"),
          q.var("spellbook")
        )
      )
    )
  ))
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" }
  ]
}
client.query(
  Paginate(
    Join(
      Match(
        Index("spellbooks_by_owner"),
        Ref(Collection("characters"), "181388642114077184")),
      Lambda { spellbook =>
        Match(Index("spells_by_spellbook"), spellbook)
      })))
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" }
  ]
}

The events view of Join contains events for joined sets as the join filtered by when the join document was live in the source set.

client.Query(
  Paginate(
    Join(
      Match(
        Index("spellbooks_by_owner"),
        Ref(Collection("characters"), "181388642114077184")),
      Lambda(
        "spellbook",
        Match(Index("spells_by_spellbook"), Var("spellbook")))),
    events: true));
{
  "data": [
    {
      "ts": 1509244539547758,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" }
    },
    {
      "ts": 1509244539547758,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" }
    }
  ]
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "join": {
              "match": { "index": "spellbooks_by_owner" },
              "terms": { "@ref": "classes/characters/181388642114077184" }
            },
            "with": {
              "lambda": "spellbook",
              "expr": {
                "match": { "index": "spells_by_spellbook" },
                "terms": { "var": "spellbook" }
              }
            }
          },
          "events": true
        }'
HTTP/1.1 200 OK
{
  "resource": {
    "data": [
      {
        "ts": 1509244539547758,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642046968320" }
      },
      {
        "ts": 1509244539547758,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642071085568" }
      }
    ]
  }
}
result, _ := client.Query(
    f.Paginate(
        f.Events(
            f.Join(
                f.MatchTerm(
                    f.Index("spellbooks_by_owner"),
                    f.RefCollection(f.Collection("characters"), "181388642114077184"),
                ),
                f.Lambda(
                    "spellbook",
                    f.MatchTerm(
                        f.Index("spells_by_spellbook"),
                        f.Var("spellbook"),
                    ),
                ),
            ),
        ),
    ),
)

fmt.Println(result)
map[data:[
  map[ts:1509244539547758 action:add document:{181388642046968320 0xc420285c60 <nil>}]
  map[ts:1509244539547758 action:add document:{181388642071085568 0xc420285f20 <nil>}]
]]
client.query(
  Paginate(
      Join(
        Match(
          Index(Value("spellbooks_by_owner")),
          Ref(Collection("characters"), "181388642114077184")),
        Lambda(
          Value("spellbook"),
          Match(
            Index(Value("spells_by_spellbook")),
            Var("spellbook")))))
    .events(Value(true)));
{
  "data": [
    {
      ts: 1509244539547758,
      action: "create",
      resource: ref(id = "181388642046968320", collection = ref(id="spells", collection = ref(id = "collections")))
    },
    {
      ts: 1509244539547758,
      action: "create",
      resource: ref(id = "181388642071085568", collection = ref(id="spells", collection = ref(id = "collections")))
    }
  ]
}
client.query(
  q.Paginate(
    q.Events(
      q.Join(
        q.Match(
          q.Index('spellbooks_by_owner'),
          q.Ref(q.Collection('characters'), '181388642114077184'),
        ),
        q.Index('spells_by_spellbook'),
      )
    )
  )
)
.then((ret) => console.log(ret))
{ data:
  [  { ts: 1527095186458101,
       action: 'add',
       document:
        Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))) },
     { ts: 1527095186458101,
       action: 'add',
       document:
        Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))) } ] }
client.query(
  q.paginate(
    q.join(
      q.match(
        q.index("spellbooks_by_owner"),
        q.ref(q.collection("characters"), "181388642114077184")
      ),
      q.lambda_(
        "spellbook",
        q.match(
          q.index("spells_by_spellbook"),
          q.var("spellbook")
        )
      )
    ),
    events=True
  ))
{
  "data": [
    {
      "ts": 1509244539547758,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" }
    },
    {
      "ts": 1509244539547758,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" }
    }
  ]
}
client.query(
  Paginate(
    Join(
      Match(
        Index("spellbooks_by_owner"),
        Ref(Collection("characters"), "181388642114077184")),
      Lambda { spellbook =>
        Match(Index("spells_by_spellbook"), spellbook)
      }),
    events = true))
{
  "data": [
    {
      "ts": 1509244539547758,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" }
    },
    {
      "ts": 1509244539547758,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" }
    }
  ]
}

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!