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对象中提取这些键的值。

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 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中获取此示例,并将其存储在其自己的对象中,以了解我们可以对数组执行的操作。

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.

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。由于所有这些值都是数字,因此我们避免将它们包装在字符串中。 最后,在添加结果之后,我们查询数据(显示查询的第一部分)。

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.

在基于子文档查询文档之前,我们将在此日常练习中,再添加两个遵循此格式的文档以及练习的子文档。

查询子文档 (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 入门

azure 入门_Azure Cosmos DB中的子文档入门相关推荐

  1. azure 入门_Azure Cosmos DB入门

    azure 入门 In the past two years, we've seen an explosion in growth with document-oriented databases l ...

  2. azure db 设置时区_在Azure Cosmos DB中应用字段运算符和对象

    azure db 设置时区 Since we will sometimes require removing documents in Azure Cosmos DB, we'll want to b ...

  3. azure api 管理_Azure Cosmos DB和MongoDB API入门

    azure api 管理 In the previous article on Azure Cosmos DB, we reviewed NoSQL concepts and how to integ ...

  4. azure云数据库_在Azure Cosmos DB中使用PowerShell创建和删除数据库

    azure云数据库 Our testing or development may call for dynamic creation on the database level for Azure C ...

  5. 一打开控制台窗口就中文输入_Lightconverse灯光设计软件中文电子文档教程免费分享...

     点击上面"灯光音响学习网" 即可免费订阅! Lightconverse是一款乌克兰的灯光设计软件,逼真度优于WYSIWYG,素材丰富.有强大的自定义灯库,让离线编程更加方便且不费 ...

  6. sql azure 语法_Azure Data Studio中SQL代码段

    sql azure 语法 This article will fully cover the code snippet SQL developer productivity feature in Az ...

  7. sql azure 语法_Azure Data Studio中SQL Server架构比较扩展

    sql azure 语法 This article explores the SQL Server Schema Compare extension in the Azure Data Studio. ...

  8. sql azure 语法_Azure SQL Server中的CREATE DATABASE语句概述

    sql azure 语法 In this article, we will review CREATE DATABASE statement in the Azure SQL database wit ...

  9. MongoDB中的子文档批量更新

    Mongodb 3.6版本的福利好吧! 谢谢观看,转载请标注来源Thanks!©PerfumerKarma 官网文档地址: https://docs.mongodb.com/manual/refere ...

最新文章

  1. String Manipulation
  2. hdu6380(2018 “百度之星”程序设计大赛 - 初赛(B))
  3. java 异常抛出 顺序_java异常抛出时间顺序
  4. 公开征集 | 每个人都可以成为 COSCon'21 主论坛的开源明星
  5. 一壶 100℃ 的开水从多高倒进嘴里不会觉得烫?
  6. NAACL 2019 | 怎样生成语言才能更自然,斯坦福提出超越Perplexity的评估新方法
  7. c语言汉字属于什么类型_空气悬浮风机在风机中属于什么类型?
  8. PMP是什么,PMP最难的是哪些内容?
  9. python队列及其实现
  10. java启动项目报错,org.apache.catalina.lifecycleException..............以及解决方案
  11. kendotabstrip 动态加tab_加你的好友这么多,为什么偏偏记不住我这名微商?
  12. 架设自己的邮件服务器
  13. 没键盘计算机能启动吗,实现PC电脑无键盘鼠标开机
  14. 苹果App Store审核指南中文翻译(更新)
  15. Mac OS X 10.8.5升级到更高版本的方法
  16. python2.7运行报警告:UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode
  17. 补充Live555推实时流
  18. OpenInfra峰会首度落地中国,中国OpenStack方兴未艾
  19. PixiJS学习(6)文本
  20. 【知识蒸馏】Knowledge Review

热门文章

  1. 计算机应用基础问题,计算机应用基础常见问题
  2. MYSQL中的日期转换
  3. python应用:爬虫框架Scrapy系统学习第三篇——初识scrapy
  4. 配置 MySQL 服务器容器
  5. HTML input 控件
  6. Vue 组件 data为什么是函数
  7. Python实现常用的数据结构
  8. java多条件不定条件查询
  9. COM 学习(五)——编译、注册、调用
  10. 在64位机器上无法调用迅雷的问题