Difference

Difference( source, diff, ... )
Difference( source, diff, ... )
Difference( source, diff, ... )
Difference( source, diff, ... )
difference( source, diff, ... )
difference( source, diff, ... )
Difference( source, diff, ... )
Difference( source, diff, ... )

Description

The Difference function compares the source, which can be an Array or Set, with the item(s) provided by diff, and returns all of the items that exist in source that do not exist in diff.

Parameters

Argument Type Definition and Requirements

source

The source array or set to be compared. The type of source must match all of the items in diff.

diff

One or more difference array or setref objects. The type of all items in diff must match the type of source.

Returns

When source is an array, an array of the items in source that are missing from diff.

When source is a setref, a setref object of the items in source that are missing from diff.

Examples

The following query takes the source SetRef (i.e. set reference) which is created by locating the search term "fire" in the index named "spells_by_element" and removing all difference SetRef which was created by locating the search term "water" in the Index named "spells_by_element". The Paginate function materialized the results of the Difference operation in an array of type Page.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "difference": [
              {
                "match": { "index": "spells_by_element" },
                "terms": "fire"
              },
              {
                "match": { "index": "spells_by_element" },
                "terms": "water"
              }
            ]
          }
        }'
client.Query(
  Paginate(
    Difference(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water"))));
System.out.println(
    client.query(
      Paginate(
        Difference(
          Match(Index(Value("spells_by_element")), Value("fire")),
          Match(Index(Value("spells_by_element")), Value("water"))))
    ).get());
result, _ := client.Query(
    f.Paginate(
        f.Difference(
            f.MatchTerm(f.Index("spells_by_element"), "fire"),
            f.MatchTerm(f.Index("spells_by_element"), "water"),
        ),
    ),
)

fmt.Println(result)
client.query(
  Paginate(
    Difference(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water"))))
client.query(
  q.paginate(
    q.difference(
      q.match(q.index("spells_by_element"), "fire"),
      q.match(q.index("spells_by_element"), "water")
    )
  ))
$client.query do
  paginate difference(match(index('spells_by_element'), 'fire'),
                      match(index('spells_by_element'), 'water'))
end
client.query(
    Paginate(
        Difference(
            Match(
                index: Index("spells_by_element"),
                terms: "fire"
            ),
            Match(
                index: Index("spells_by_element"),
                terms: "water"
            )
        )
    )
)
client.query(
  q.Paginate(
    q.Difference(
      q.Match(q.Index('spells_by_element'), 'fire'),
      q.Match(q.Index('spells_by_element'), 'water'),
    )
  )
)
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
  "resource": {
    "data": [ { "@ref": "classes/spells/181388642046968320" } ]
  }
}
{ "data": [ { "@ref": "classes/spells/181388642046968320" } ] }
{
  data: [
    ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections")))
  ]
}
map[data:[{181388642046968320 0xc420239980 <nil>}]]
{ "data": [ { "@ref": "classes/spells/181388642046968320" } ] }
{ "data": [ { "@ref": "classes/spells/181388642046968320" } ] }
{ "data": [ { "@ref": "classes/spells/181388642046968320" } ] }
{ "data": [ { "@ref": "classes/spells/181388642046968320" } ] }
{ data:
   [ Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))) ] }

The following query is similar to the example above, but it returns document events instead of the index tuples.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "paginate": {
            "difference": [
              {
                "match": { "index": "spells_by_element" },
                "terms": "fire"
              },
              {
                "match": { "index": "spells_by_element" },
                "terms": "water"
              }
            ]
          },
          "events": true
        }'
client.Query(
  Paginate(
    Difference(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water")),
    events: true));
System.out.println(
        client.query(
           Paginate(
             Events(
               Difference(
                 Match(Index(Value("spells_by_element")), Value("fire")),
                 Match(Index(Value("spells_by_element")), Value("water"))
               )
             )
           )
        ).get());
result, _ := client.Query(
    f.Paginate(
        f.Events(
            f.Difference(
                f.MatchTerm(f.Index("spells_by_element"), "fire"),
                f.MatchTerm(f.Index("spells_by_element"), "water"),
            ),
        ),
    ),
)

fmt.Println(result)
client.query(
  Paginate(
    Difference(
      Match(Index("spells_by_element"), "fire"),
      Match(Index("spells_by_element"), "water")),
    events = true))
client.query(
  q.paginate(
    q.difference(
      q.match(q.index("spells_by_element"), "fire"),
      q.match(q.index("spells_by_element"), "water")
    ),
    events=True
  ))
$client.query do
  paginate difference(match(index('spells_by_element'), 'fire'),
                      match(index('spells_by_element'), 'water')),
           events: true
end
client.query(
    Paginate(
        Difference(
            Match(
                index: Index("spells_by_element"),
                terms: "fire"
            ),
            Match(
                index: Index("spells_by_element"),
                terms: "water"
            )
        ),
        events: true
    )
)
client.query(
  q.Paginate(
    q.Events(
      q.Difference(
        q.Match(q.Index('spells_by_element'), 'fire'),
        q.Match(q.Index('spells_by_element'), 'water'),
      )
    )
  )
)
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{
  "resource": {
    "data": [
      {
        "ts": 1509244539203043,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642046968320" }
      },
      {
        "ts": 1509244539223511,
        "action": "create",
        "resource": { "@ref": "classes/spells/181388642071085568" }
      }
    ]
  }
}
{
  "data": [
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" }
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" }
    }
  ]
}
{
  data: [
    {
      ts: 1527002946726063,
      action: "add",
      document: ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections")))
    },
    {
      ts: 1527002946795516,
      action: "add",
      document: ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections")))
    }
  ]
}
map[data:[
  map[ts:1509244539203043 action:add document:{181388642046968320 0xc420261160 <nil>}]
  map[ts:1509244539223511 action:add document:{181388642071085568 0xc420261420 <nil>}]
]]
{
  "data": [
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" }
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" }
    }
  ]
}
{
  "data": [
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" }
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" }
    }
  ]
}
{
  "data": [
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" }
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" }
    }
  ]
}
{
  "data": [
    {
      "ts": 1509244539203043,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642046968320" }
    },
    {
      "ts": 1509244539223511,
      "action": "create",
      "resource": { "@ref": "classes/spells/181388642071085568" }
    }
  ]
}
{ data:
   [ { ts: 1526677776479051,
       action: 'add',
       document:
        Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))) },
     { ts: 1526677776479051,
       action: 'add',
       document:
        Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))) } ] }

The following query demonstrates how various arrays are compared:

Value result = await client.Query(
  Arr(
    Difference(Arr("A", "B", "C"), Arr("B", "C", "D")),
    Difference(Arr("B", "C", "D"), Arr("A", "B", "C")),
    Difference(Arr("A", "B", "C"), Arr("C", "B", "A"))
  )
);
result, err := client.Query(
  f.Arr{
    f.Difference(f.Arr{"A", "B", "C"}, f.Arr{"B", "C", "D"}),
    f.Difference(f.Arr{"B", "C", "D"}, f.Arr{"A", "B", "C"}),
    f.Difference(f.Arr{"A", "B", "C"}, f.Arr{"C", "B", "A"})})
fmt.Println(result)
System.out.println(
    client.query(
        Arr(
            Difference(
                Arr(Value("A"), Value("B"), Value("C")),
                Arr(Value("B"), Value("C"), Value("D"))
            ),
            Difference(
                Arr(Value("B"), Value("C"), Value("D")),
                Arr(Value("A"), Value("B"), Value("C"))
            ),
            Difference(
                Arr(Value("A"), Value("B"), Value("C")),
                Arr(Value("C"), Value("B"), Value("A"))
            )
        )
    ).get());
client.query([
  q.Difference(['A', 'B', 'C'], ['B', 'C', 'D']),
  q.Difference(['B', 'C', 'D'], ['A', 'B', 'C']),
  q.Difference(['A', 'B', 'C'], ['C', 'B', 'A']),
])
.then((ret) => console.log(ret))
print(client.query(
  [
    q.difference(['A', 'B', 'C'], ['B', 'C', 'D']),
    q.difference(['B', 'C', 'D'], ['A', 'B', 'C']),
    q.difference(['A', 'B', 'C'], ['C', 'B', 'A']),
  ]
))
puts $client.query {[
  difference(['A', 'B', 'C'], ['B', 'C', 'D']),
  difference(['B', 'C', 'D'], ['A', 'B', 'C']),
  difference(['A', 'B', 'C'], ['C', 'B', 'A']),
]}
println(Await.result(
  client.query(
    Arr(
      Difference(Arr("A", "B", "C"), Arr("B", "C", "D")),
      Difference(Arr("B", "C", "D"), Arr("A", "B", "C")),
      Difference(Arr("A", "B", "C"), Arr("C", "B", "A"))
    )
  ),
  5.seconds
))
Not available in this language yet.
Not available in this language yet.
[
  [
    "A"
  ],
  [
    "D"
  ],
  []
]
[[A] [D] []]
[["A"], ["D"], []]
[ [ 'A' ], [ 'D' ], [] ]
[['A'], ['D'], []]
A
D
[["A"], ["D"], []]

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!