Skip to content

Commit c7e0142

Browse files
rakhi-prathapsimon-dew
authored andcommitted
[DOC-10590] Add EXCLUDE clause to SELECT (#382)
1 parent a4661a7 commit c7e0142

File tree

6 files changed

+114
-2
lines changed

6 files changed

+114
-2
lines changed
5.77 KB
Loading
4.34 KB
Loading
1.74 KB
Loading

modules/n1ql/pages/n1ql-language-reference/select-syntax.adoc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
:page-topic-type: reference
66

77
:expression: xref:n1ql-language-reference/index.adoc#N1QL_Expressions
8+
:string-expression: xref:n1ql-language-reference/literals.adoc#strings
89
:hints: xref:n1ql-language-reference/optimizer-hints.adoc
910
:conventions: xref:n1ql-language-reference/conventions.adoc
1011
:number: xref:n1ql-language-reference/literals.adoc#numbers
@@ -81,7 +82,7 @@ image::n1ql-language-reference/alias.png["Syntax diagram", align=left]
8182

8283
[subs="normal"]
8384
----
84-
select-clause ::= 'SELECT' {hints}[hint-comment]? <<projection>>
85+
select-clause ::= 'SELECT' {hints}[hint-comment]? <<projection>> <<exclude-clause>>?
8586
----
8687

8788
image::n1ql-language-reference/select-clause.png["Syntax diagram", align=left]
@@ -109,6 +110,21 @@ path ::= {identifier}[identifier] ( '[' {expression}[expr] ']' )* ( '.' {identif
109110

110111
image::n1ql-language-reference/path.png["Syntax diagram", align=left]
111112

113+
[#exclude-clause,reftext="exclude-clause",subs="normal"]
114+
----
115+
exclude-clause ::= 'EXCLUDE' <<exclude-term>> ( ',' <<exclude-term>> )*
116+
----
117+
118+
image::n1ql-language-reference/exclude-clause.png["Syntax diagram", align=left]
119+
120+
[#exclude-term,reftext="exclude-term",subs="normal"]
121+
----
122+
exclude-term ::= {identifier}[identifier] | {string-expression}[string-expr]
123+
----
124+
image::n1ql-language-reference/exclude-term.png["Syntax diagram", align=left]
125+
126+
[#hints,reftext="hints",subs="normal"]
127+
112128
[[from-clause,from-clause]]
113129
== FROM Clause
114130

modules/n1ql/pages/n1ql-language-reference/selectclause.adoc

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ image::n1ql-language-reference/select-clause.png["Syntax diagram", align=left]
3434
[horizontal.compact]
3535
hint-comment:: <<hint-comment>> icon:caret-down[]
3636
projection:: <<sec_Arguments>> icon:caret-down[]
37+
exclude-clause:: <<sec_ExcludeClause>> icon:caret-down[]
3738

3839
[#hint-comment]
3940
=== Optimizer Hints
@@ -155,6 +156,47 @@ If you do not explicitly give a field an alias, it is given an _implicit alias_.
155156

156157
An implicit or explicit alias is returned in the result set, unless you suppress it using the <<raw-element-value,RAW keyword>>.
157158

159+
[#sec_ExcludeClause]
160+
=== EXCLUDE Clause
161+
162+
[source,ebnf]
163+
----
164+
include::partial$grammar/dql.ebnf[tag=exclude-clause]
165+
----
166+
image::n1ql-language-reference/exclude-clause.png["Syntax diagram", align=left]
167+
168+
The EXCLUDE clause removes specific fields from your query's result set.
169+
170+
Instead of listing every field you want to include in the SELECT statement, use EXCLUDE to specify only the ones you want to omit.
171+
This is particularly useful when you use the star expression (`{asterisk}`) to select all fields, but want to exclude a few.
172+
173+
The clause consists of one or more <<exclude-term, EXCLUDE terms>>, separated by commas.
174+
175+
[[exclude-term]]
176+
==== EXCLUDE Term
177+
[source,ebnf]
178+
----
179+
include::partial$grammar/dql.ebnf[tag=exclude-term]
180+
----
181+
image::n1ql-language-reference/exclude-term.png["Syntax diagram", align=left]
182+
183+
An EXCLUDE term is a field that you want to exclude from the final projection.
184+
It must exactly match the field name or alias as it appears in the projection.
185+
Refer to <<ex-exclude-clause>>.
186+
187+
An EXCLUDE term can be:
188+
189+
* An identifier, such as `hotel.name`.
190+
* A string expression with one or more fields separated by commas, such as `"hotel.name, address"`.
191+
192+
[NOTE]
193+
====
194+
* If the field has an alias, you must use the alias as the term.
195+
* When your query includes only one FROM term, fields are automatically qualified with it.
196+
You can use the field name without the full identifier.
197+
For example, `name` instead of `hotel.name`.
198+
====
199+
158200
[#sec_BestPractices]
159201
== Best Practices
160202

@@ -632,6 +674,52 @@ FROM hotel LIMIT 5;
632674
<.> With a select expression, you may optionally include the keyspace name; in either case, the keyspace name is not added to the results.
633675
====
634676

677+
[[ex-exclude-clause]]
678+
.SELECT with an EXCLUDE clause
679+
====
680+
.Query
681+
[source,sqlpp]
682+
----
683+
SELECT * EXCLUDE reviews,h.public_likes,"geo,description"
684+
FROM `travel-sample`.inventory.hotel h
685+
ORDER BY meta().id LIMIT 1;
686+
----
687+
688+
.Results
689+
[source,json]
690+
----
691+
[
692+
{
693+
"h": {
694+
"address": "Capstone Road, ME7 3JE",
695+
"alias": null,
696+
"checkin": null,
697+
"checkout": null,
698+
"city": "Medway",
699+
"country": "United Kingdom",
700+
"directions": null,
701+
"email": null,
702+
"fax": null,
703+
"free_breakfast": true,
704+
"free_internet": false,
705+
"free_parking": true,
706+
"id": 10025,
707+
"name": "Medway Youth Hostel",
708+
"pets_ok": true,
709+
"phone": "+44 870 770 5964",
710+
"price": null,
711+
"state": null,
712+
"title": "Gillingham (Kent)",
713+
"tollfree": null,
714+
"type": "hotel",
715+
"url": "http://www.yha.org.uk",
716+
"vacancy": true
717+
}
718+
}
719+
]
720+
----
721+
====
722+
635723
[#sec_RelatedLinks]
636724
== Related Links
637725

modules/n1ql/partials/grammar/dql.ebnf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ anchor-select ::= select
7171
/* tag::recursive-select-term[] */
7272
recursive-select-term ::= select-term
7373
/* end::recursive-select-term[] */
74+
7475
/* SELECT Clause */
7576

7677
/* tag::select-clause[] */
77-
select-clause ::= 'SELECT' hint-comment? projection
78+
select-clause ::= 'SELECT' hint-comment? projection exclude-clause?
7879
/* end::select-clause[] */
7980

8081
hint-comment ::= [https://github.com/couchbaselabs/docs-devex/blob/release/8.0/modules/n1ql/partials/grammar/hints.ebnf]
@@ -92,6 +93,13 @@ result-expr ::= ( path '.' )? '*' | expr ( 'AS'? alias )?
9293
path ::= identifier ( '[' expr ']' )* ( '.' identifier ( '[' expr ']' )* )*
9394
/* end::path[] */
9495

96+
/* tag::exclude-clause[] */
97+
exclude-clause ::= 'EXCLUDE' exclude-term ( ',' exclude-term )*
98+
/* end::exclude-clause[] */
99+
100+
/* tag::exclude-term[] */
101+
exclude-term ::= identifier | string-expression
102+
/* end::exclude-term[] */
95103

96104
/* FROM Clause */
97105

0 commit comments

Comments
 (0)