Using built-in array functions
The previous query made sure that it only processes each videogame
document that has a level
property defined at the root level and its value is an array. However, it is possible to have a new videogame
document that declares an empty array ([]
) as the value for the level
property because the video game doesn't have defined levels yet.
The next query is a new version of the previous query that takes advantage of the ARRAY_LENGTH
built-in function. This function returns the number of elements of the array expression received as an argument. The query makes sure that the level
property is an array and that it contains at least one element. The results for the query will be the same that were shown for its previous version. The code file for the sample is included in the learning_cosmos_db_03_01
folder in the sql_queries/videogame_1_07.sql
file:
SELECT v.id AS videoGameId, v.name AS videoGameName, v.levels[0] AS firstLevel FROM Videogames v WHERE IS_ARRAY...