In DocumentDB SQL, the SELECT clause additionally supports scalar expressions like constants, mathematics expressions, logical expressions, and so forth. Normally, scalar queries are hardly ever used, due to the fact they don't virtually question documents in the series, they just examine expressions. But it is nevertheless helpful to use scalar expression queries to analyze the basics, a way to use expressions and shape JSON in a question, and these ideas practice without delay to the actual queries you may be going for walks against files in a collection.
Let’s test an instance which incorporates multiple scalar queries.
In the Query Explorer, choose just the textual content to be finished and click on ‘Run’. Let's run this first one.
SELECT "Hello"
When the above query is executed, it produces the subsequent output.
[
{
"$1": "Hello"
}
]
This output may appearance a piece puzzling, so allow's damage it down.
- First, as we saw in the final demo, query effects are always contained in square brackets due to the fact they're lower back as a JSON array, even outcomes from scalar expression queries like this one which most effective returns a single report.
- We have an array with one document in it, and that document has a unmarried assets in it for the single expression inside the SELECT declaration.
- The SELECT declaration does not offer a name for this assets, hence DocumentDB vehicle generates one the usage of $1.
- This is generally now not what we need, that's why we can use AS to alias the expression inside the query, which units the assets call within the generated file the manner you'd adore it to be, phrase, in this example.
SELECT "Hello" AS word
When the above query is performed, it produces the subsequent output.
[
{
"word": "Hello"
}
]
Similarly, following is some other simple query.
SELECT ((2 + 11 % 7)-2)/3
The question retrieves the subsequent output.
[
{
"$1": 1.3333333333333333
}
]
Let’s test another example of shaping nested arrays and embedded items.
SELECT
{
"words1":
["Hello", "World"],
"words2":
["How", "Are", "You?"]
} AS allWords
When the above query is carried out, it produces the subsequent output.
[
{
"allWords": {
"words1": [
"Hello",
"World"
],
"words2": [
"How",
"Are",
"You?"
]
}
}
]