azure cli,azure 入門_Azure Cosmos DB中的子文檔入門

 2023-10-18 阅读 22 评论 0

摘要:azure 入門 As we’ve worked with Azure Cosmos DB, we’ve seen that we can store data within fields and the fields of each document don’t always have to match – though we still want some organization for querying. The fields and values storage becomes us

azure 入門

As we’ve worked with Azure Cosmos DB, we’ve seen that we can store data within fields and the fields of each document don’t always have to match – though we still want some organization for querying. The fields and values storage becomes useful when working with object-oriented languages as these fields can be keys that we use with values that we extract as properties. For an example, the below PowerShell line creates a JSON document in an object and we can see that we can extract the values of these keys in the JSON object.

在使用Azure Cosmos DB時,我們已經看到我們可以在字段中存儲數據,并且每個文檔的字段不一定總是匹配-盡管我們仍然希望某些組織進行查詢。 當使用面向對象的語言時,字段和值的存儲將變得很有用,因為這些字段可以作為我們與提取為屬性的值一起使用的鍵。 例如,下面的PowerShell行在一個對象中創建了一個JSON文檔,我們可以看到我們可以在JSON對象中提取這些鍵的值。

azure cli?

Sometimes our data have hierarchies or related data to what we have stored and we must keep these data grouped in a manner that related to our existing data. We’ll look at how we can group related and hierarchy data together using arrays and subdocuments.

有時,我們的數據具有層次結構或與我們存儲的數據相關的數據,我們必須以與現有數據相關的方式對這些數據進行分組。 我們將研究如何使用數組和子文檔將相關數據和層次結構數據分組在一起。

在Azure Cosmos DB中創建子文檔和數組 (Creating Subdocuments and Arrays in Azure Cosmos DB)

We’ve used documents in Azure Cosmos DB to store data values of fields directly in our documents. For an example, when we query our collection for Jump Rope/Pushup Circuit exercises, we see the fields of Jump Rope and Pushups. Using the same scenario of fitness, we may have multiple fields that relate to the same document that should be within a field, like a rotation of exercises that are part of a routine that are within the document. We can create a subdocument for this. A subdocument is a document within a document and this differs from the SQL relationship approach, as rather than create a situation with JOINs, we store the related data in this case in a subdocument within our document.

azure 虛擬機。 我們已使用Azure Cosmos DB中的文檔將字段的數據值直接存儲在我們的文檔中。 例如,當我們查詢集合中的跳繩/俯臥撐練習時,我們會看到跳繩和俯臥撐的字段。 使用相同的適應性場景,我們可能有多個字段與應屬于該字段的同一文檔相關聯,例如輪換練習是該文檔中例程的一部分。 我們可以為此創建一個子文檔。 子文檔是文檔中的文檔,它不同于SQL關系方法,因為我們不是在使用JOIN創建情況,而是在這種情況下將相關數據存儲在文檔中的子文檔中。

We can also store information within arrays in Azure Cosmos DB. Arrays group related information together identified by a field. From our PowerShell example, we that the field Weight stores data in a two-element array – 250 and 225. Let’s take this example from PowerShell and store it in its own object to see what we can do with arrays.

我們還可以在Azure Cosmos DB中將信息存儲在陣列中。 數組將由字段標識的相關信息組合在一起。 在PowerShell示例中,字段Weight將數據存儲在兩個元素的數組中(250和225)。讓我們從PowerShell中獲取此示例,并將其存儲在其自己的對象中,以了解我們可以對數組執行的操作。

azure docker?

We see that we can get the values from an array by directly referring to them in their element position (always starts at 0). As we see in our example, our first element in our array is 250. We can also loop through each element of our array. Both of these items stored refer to Weight – 250 being the first element and 225 being the second element. Because this involves exercises in sets, we can deduce that the first exercise involved a weight of 250 and the second involved a weight of 225. Compare storing data this way to having another row of data or having a field which specifies set one and set two. Using an array here is more intuitive and as we see, easier to work with when using object-oriented languages.

我們看到可以通過直接在元素位置(始終從0開始)引用它們來從數組中獲取值。 正如我們在示例中看到的那樣,數組中的第一個元素是250。我們還可以遍歷數組中的每個元素。 存儲的所有這兩項均指權重– 250是第一個元素,225是第二個元素。 因為這涉及集合中的練習,所以我們可以推斷出第一個練習的權重為250,第二個練習的權重為225。將以這種方式存儲數據與另一行數據或具有指定第一個和第二個字段的字段進行比較。 在這里使用數組更直觀,并且正如我們所看到的,在使用面向對象的語言時更容易使用。

The strength of NoSQL databases, like Azure Cosmos DB and MongoDB, are their ability to provide data easily to object-oriented languages on a document-level. We want to avoid querying across multiple objects to get data and instead query on the document level and get the data we need within it.

azure blob、 NoSQL數據庫(例如Azure Cosmos DB和MongoDB)的優勢在于它們能夠輕松地在文檔級向面向對象的語言提供數據的能力。 我們希望避免在多個對象之間進行查詢以獲取數據,而是在文檔級別上進行查詢并在其中獲取所需的數據。

Using the example exercise routine of HST, we’ll create an array with subdocuments that list ten exercises that are a part of this routine that follow a format of the number of times the exercise was performed (sets), the weight of each set, and the total repetitions for our exercise. Notice the structure of our insert into our Azure Cosmos DB and how the subdocuments inside the array match the format of field and value. We’ll also see an array object for weights where there are two sets – for instance, we have two values for the weight for bench press of 250 and 225. Because all of these values are numbers, we avoid wrapping them in strings. Finally, after we add our result, we query the data (the first part of the query is shown).

使用HST的示例鍛煉例程,我們將創建一個包含子文檔的數組,該子文檔列出了該例程一部分的十個鍛煉,它們遵循鍛煉次數(組),每組重量,以及我們鍛煉的總重復次數。 請注意,插入到Azure Cosmos DB中的結構以及數組內的子文檔如何與字段和值的格式匹配。 我們還將看到具有兩個權重的數組對象-例如,對于臺式臥推,我們有兩個權重值250和225。由于所有這些值都是數字,因此我們避免將它們包裝在字符串中。 最后,在添加結果之后,我們查詢數據(顯示查詢的第一部分)。

azure monitor?

Let’s look at our insert query again and think about why this organizes our data better than trying to add these all as fields. Because of this structure, we know the exercise, the sets performed, the weight of each set, and the total repetitions. We could structure our repetitions by the count of each (two-value array) or the total like we do in the document we added. Ultimately, these exercises relate to this day and this type of workout and we group them by subdocuments for organization in our Azure Cosmos DB. We could add them all as individual fields in the main document and avoid using the subdocument, but this would make the organization difficult and our queries difficult to understand, especially if we have repetitions that may differ on some days – such as a day where we do 30 repetitions for one exercise, but 20 for another exercise. When we group data in subdocuments, we want to think about how they data will be queried along with what other fields may be extracted as well. Our NoSQL database may not the final destination (or source) and when we extract these data for applications, reports, or even SQL Engines, we have to think about how we group our data for those platforms.

讓我們再次看一下插入查詢,并思考為什么這樣做比嘗試將所有數據都添加為字段更好地組織了我們的數據。 由于這種結構,我們知道練習,進行的練習,每個練習的權重以及總重復次數。 我們可以通過每個(二值數組)或總數的計數來構造重復,就像我們在添加的文檔中所做的那樣。 最終,這些練習與這一天和這種類型的鍛煉有關,我們將它們按子文檔分組,以在Azure Cosmos DB中進行組織。 我們可以將它們全部添加為主文檔中的各個字段,而避免使用子文檔,但這將使組織變得困難并且我們的查詢難以理解,特別是如果我們重復的次數在某些天(例如,當日我們一個練習重復30次,但另一次練習重復20次。 在將數據分組到子文檔中時,我們想考慮如何查詢它們的數據以及可能提取的其他字段。 我們的NoSQL數據庫可能不是最終的目的地(或源),當我們為應用程序,報表甚至SQL引擎提取這些數據時,我們必須考慮如何對這些平臺的數據進行分組。

Before we query documents based on subdocuments, we’ll add two more documents following this format with subdocuments of exercises within our daily exercise.

azure開發。 在基于子文檔查詢文檔之前,我們將在此日常練習中,再添加兩個遵循此格式的文檔以及練習的子文檔。

查詢子文檔 (Querying Subdocuments)

Querying documents by using data within subdocuments in Azure Cosmos DB follows a similar pattern to querying data in documents. Like querying a field that exists in one document, but not another, our queries will return documents that have the subdocuments of the data we’re querying. One technique we can use for querying subdocuments is the dot notation since the subdocuments fields function as properties for the field object that holds the subdocument. If we look at our documents we created, we’ll see the exercise field and within that field, a list of subdocuments with fields. We can therefore use the dot notation of DocumentField.SubdocumentField and apply a filter. In our below example we apply this technique – our DocumentField is exercise and our SubdocumentField is Set. From there, we filter on “Pullups” and avoid returning all the fields except the date and time of the routine.

通過使用Azure Cosmos DB中子文檔中的數據查詢文檔遵循與查詢文檔中數據相似的模式。 就像查詢一個文檔中存在的字段,而不查詢另一個文檔中的字段一樣,我們的查詢將返回包含我們正在查詢的數據的子文檔的文檔。 由于子文檔字段用作保存子文檔的字段對象的屬性,因此可用于查詢子文檔的一種技術是點符號。 如果查看我們創建的文檔,我們將看到“練習”字段,在該字段中將看到帶有字段的子文檔列表。 因此,我們可以使用DocumentField.SubdocumentField的點表示法并應用過濾器。 在下面的示例中,我們應用了此技術–我們的DocumentFieldexercise,SubdocumentFieldSet 。 從那里開始,我們對“上拉”進行過濾,并避免返回例程日期和時間以外的所有字段。

We can also apply similar design logic to the output of our queries. Suppose that we want to only see the Sets and Weights and exclude everything else. We can use the dot notation on our output options to filter out the _id field (which recall returns by default unless excluded) and the Set and Weight. Since the repetitions for these three documents is the same, we’ll exclude them by not adding them to our output options.

我們還可以將類似的設計邏輯應用于查詢的輸出。 假設我們只想查看“集合”和“權重”,而排除其他所有內容。 我們可以在輸出選項上使用點符號來過濾_id字段(默認情況下,除非排除,否則將調用返回值)和Set and Weight。 由于這三個文檔的重復相同,因此我們通過不將其添加到輸出選項中來排除它們。

Like we used operators with filtering for documents in Azure Cosmos DB, we can apply these operators in our subdocuments. In the below queries, we first look for documents where we used a weight greater than 400 for the type of HST (all should return). In the next query, we look for a document where used a weight less than 100 for the type of HST (none should return).

就像我們使用運算符對Azure Cosmos DB中的文檔進行過濾一樣,我們可以在子文檔中應用這些運算符。 在以下查詢中,我們首先查找對HST類型使用權重大于400的文檔(所有文檔應返回)。 在下一個查詢中,我們查找HST類型使用的權重小于100的文檔(均不返回)。

結論 (Conclusion)

As we’ve seen, we can store subdocuments in Azure Cosmos DB that are related to existing documents. In practice, one popular example is a person’s list of addresses as a subdocument within the document of the person document – this can be an entire list of addresses. In our example, we stored a type of exercise program as an array of subdocuments. While this can be useful to group related information, relative to how we plan to query our data, we’ll want to design our subdocument to align with these queries, like we want our document to as well. For instance, our example would work well if our queries will always pull the full lists of exercises when querying for HST exercise days. This highlights the most important point that we want to design our data model to fit around how our users will query the data in the least performance impacting manner as possible.

如我們所見,我們可以在Azure Cosmos DB中存儲與現有文檔相關的子文檔。 實際上,一個流行的示例是一個人的地址列表,作為該人文檔的文??檔中的子文檔–這可以是一個完整的地址列表。 在我們的示例中,我們將一種鍛煉程序存儲為一系列子文檔。 盡管這對于將相關信息進行分組很有用,但相對于我們計劃如何查詢數據的方式,我們還是希望將子文檔設計為與這些查詢保持一致,就像我們也希望文檔一樣。 例如,如果我們的查詢在查詢HST鍛煉日期時始終提取鍛煉的完整列表,則該示例將很好地工作。 這突出了我們要設計數據模型以適應用戶如何以對性能影響最小的方式查詢數據的最重要點。

目錄 (Table of contents)

Getting Started with Azure Cosmos DB
Updating and Querying Details in Azure Cosmos DB
Applying Field Operators and Objects in Azure Cosmos DB
Getting Started with Subdocuments in Azure Cosmos DB
Azure Cosmos DB入門
在Azure Cosmos DB中更新和查詢詳細信息
在Azure Cosmos DB中應用字段運算符和對象
Azure Cosmos DB中的子文檔入門

翻譯自: https://www.sqlshack.com/getting-started-with-subdocuments-in-azure-cosmos-db/

azure 入門

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/144341.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息