You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/n1ql/pages/n1ql-language-reference/arrayfun.adoc
+113Lines changed: 113 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1497,6 +1497,119 @@ SELECT array_star(children).age FROM contacts WHERE fname = "Dave"
1497
1497
----
1498
1498
====
1499
1499
1500
+
=== Empty Array Subscripts
1501
+
1502
+
You can use an empty array subscript (`[ ]`) to return an array that includes only defined elements.
1503
+
1504
+
The rules governing its use are as follows:
1505
+
1506
+
[cols="1a,2a"]
1507
+
|====
1508
+
| Expression | Description
1509
+
1510
+
| `field[]`
1511
+
|* If `field` is not an array, it returns `NULL`.
1512
+
* If `field` is an array, it returns the array as is.
1513
+
1514
+
1515
+
| `field[][]`
1516
+
|* If `field` is not an array, it returns `NULL`.
1517
+
* If `field` contains only unnamed arrays, it returns `field` as is.
1518
+
Otherwise, it returns `NULL`.
1519
+
1520
+
| `field[].field2`
1521
+
|* If `field` is not an array, it returns `MISSING`.
1522
+
* If `field` is an array, it extracts `field2` from each element in `field` and returns a new array with those values.
1523
+
* If `field` contains unnamed arrays, they are flattened by one level.
1524
+
Then from the resulting array, it extracts `field2` from each object where it is present.
1525
+
1526
+
| `field[][].field2`
1527
+
|* If `field` is not an array, it returns `MISSING`.
1528
+
* If every element in `field` is not an unnamed array, it returns `MISSING`.
1529
+
* If `field` contains unnamed arrays, they are flattened by one level.
1530
+
Then from the resulting array, it extracts `field2` from each object where it is present.
1531
+
1532
+
1533
+
| `field[].field2[].field3`
1534
+
|* Returns an array of `field3` values by traversing two levels of arrays.
1535
+
First it extracts `field2` from each element of `field`, then extracts `field3` from each element of the resulting `field2` array.
1536
+
* If `field` and `field2` contain unnamed arrays, they are flattened by one level.
1537
+
Then from the resulting `field2` array, it extracts `field3` from each object where it is present.
1538
+
1539
+
|====
1540
+
1541
+
NOTE: If you use more that two empty array subscripts (for example, `field[][][]`), the function considers only the first two subscripts and ignores the rest.
1542
+
1543
+
==== Example
1544
+
1545
+
====
1546
+
Given the following sample document:
1547
+
1548
+
[source,json]
1549
+
----
1550
+
{
1551
+
"a": {
1552
+
"airline": "AF",
1553
+
"airlineid": "airline_003",
1554
+
"destinationairport": "SFO",
1555
+
"distance": 2481.617376098415,
1556
+
"equipment": "320",
1557
+
"id": 3,
1558
+
"schedule": [
1559
+
{
1560
+
"day": 0,
1561
+
"flight": "AF198",
1562
+
"utc": "10:13:00"
1563
+
},
1564
+
{
1565
+
"flight": "AF250",
1566
+
"utc": "12:59:00"
1567
+
},
1568
+
{
1569
+
"day": 2,
1570
+
"flight": "AF223",
1571
+
"special_flights": [
1572
+
{
1573
+
"a": "SA"
1574
+
},
1575
+
{
1576
+
"b": [
1577
+
[
1578
+
{
1579
+
"c": "SC1"
1580
+
},
1581
+
{
1582
+
"c": "SC2"
1583
+
}
1584
+
],
1585
+
[
1586
+
{
1587
+
"c": "SC3"
1588
+
}
1589
+
]
1590
+
]
1591
+
}
1592
+
],
1593
+
"utc": "19:41:00"
1594
+
}
1595
+
],
1596
+
"sourceairport": "DFW"
1597
+
}
1598
+
}
1599
+
----
1600
+
1601
+
Here's how different array subscripts evaluate:
1602
+
1603
+
* `a.airline[]`: Returns `NULL` (not an array).
1604
+
* `a.schedule[]`: Equivalent to `a.schedule`, returns the array.
1605
+
* `a.schedule[].day`: Returns `[0, 2]`. This is in contrast to `a.schedule[*].day`, which returns `[0, null, 2]`.
0 commit comments