แก้ไข

แชร์ผ่าน


SetUnion (NoSQL query)

APPLIES TO: NoSQL

Gathers expressions in two sets and returns a set of expressions containing all expressions in both sets with no duplicates.

Syntax

SetUnion(<array_expr_1>, <array_expr_2>)

Arguments

Description
array_expr_1 An array of expressions.
array_expr_2 An array of expressions.

Return types

Returns an array of expressions.

Examples

This first example uses the function with static arrays to demonstrate the union functionality.

SELECT VALUE {
    simpleUnion: SetUnion([1, 2, 3, 4], [3, 4, 5, 6]),
    emptyUnion: SetUnion([1, 2, 3, 4], []),
    duplicatesUnion: SetUnion([1, 2, 3, 4], [1, 1, 1, 1]),
    unorderedUnion: SetUnion([1, 2, "A", "B"], ["A", 1])
}
[
  {
    "simpleUnion": [1, 2, 3, 4, 5, 6],
    "emptyUnion": [1,2,3,4],
    "duplicatesUnion": [1,2,3,4],
    "unorderedUnion": [1,2,"A","B"]
  }
]

This last example uses an item that share values within multiple array properties.

[
  {
    "name": "Malsca coat",
    "category": "seasonal-coats",
    "colors": [
      {
        "season": "Winter",
        "values": [
          "Cutty Sark",
          "Horizon",
          "Russet",
          "Fuscous"
        ]
      },
      {
        "season": "Summer",
        "values": [
          "Fuscous",
          "Horizon",
          "Tacha"
        ]
      }
    ]
  }
]

The query returns the union of the two arrays as a new property.

SELECT
    p.name,    
    SetUnion(p.colors[0].values, p.colors[1].values) AS allColors
FROM
    products p
WHERE
    p.category = "seasonal-coats"
[
  {
    "name": "Malsca coat",
    "allColors": [
      "Cutty Sark",
      "Horizon",
      "Russet",
      "Fuscous",
      "Tacha"
    ]
  }
]

Remarks

  • This function doesn't return duplicates.
  • This function doesn't use the index.

See also