【问题标题】:Azure CosmosDB Query on Sub Elements子元素上的 Azure CosmosDB 查询
【发布时间】:2017-11-15 12:20:39
【问题描述】:

我有这样的文件

   [
    {
      "id": "1",
      "name": "My Product 1",
      "variants": [
        {
          "id": 2179,
          "code": "A",
          "attributes": {
            "att_set_1": {
              "en": {
                "name": "Attribute Set 1",
                "data": [
                  {
                    "id": 919,
                    "title": "Height",
                    "label": "height_mm",
                    "v": 1200,
                    "unit": "mm"
                  },
                  {
                    "id": 921,
                    "title": "Weight",
                    "label": "weight",
                    "v": 500,
                    "unit": "kg"
                  },
                  {
                    "id": 923,
                    "title": "Other",
                    "label": "blah",
                    "v": 200,
                    "unit": "mm"
                  }
                ]
              }
            }
          }
        }
      ]
    },
    {
      "id": "2",
      "name": "My Product 2",
      "variants": [
        {
          "id": 2180,
          "code": "B",
          "attributes": {
            "att_set_1": {
              "en": {
                "name": "Attribute Set 1",
                "data": [
                  {
                    "id": 919,
                    "title": "Height",
                    "label": "height_mm",
                    "v": 1300,
                    "unit": "mm"
                  },
                  {
                    "id": 921,
                    "title": "Weight",
                    "label": "weight",
                    "v": 200,
                    "unit": "kg"
                  },
                  {
                    "id": 923,
                    "title": "Other",
                    "label": "blah",
                    "v": 200,
                    "unit": "mm"
                  }
                ]
              }
            }
          }
        }
      ]
    },
    {
      "id": "3",
      "name": "My Product 3",
      "variants": [
        {
          "id": 2181,
          "code": "C",
          "attributes": {
            "att_set_1": {
              "en": {
                "name": "Attribute Set 1",
                "data": [
                  {
                    "id": 919,
                    "title": "Height",
                    "label": "height_mm",
                    "v": 2000,
                    "unit": "mm"
                  },
                  {
                    "id": 921,
                    "title": "Weight",
                    "label": "weight",
                    "v": 999,
                    "unit": "kg"
                  },
                  {
                    "id": 923,
                    "title": "Other",
                    "label": "blah",
                    "v": 200,
                    "unit": "mm"
                  }
                ]
              }
            }
          }
        }
      ]
    }
  ]

我现在想查找其中一种变体的高度 >= 1200 且重量 >= 500 的所有产品

在本例中,这应该是我的产品 1 和我的产品 3。我的产品 2 不匹配,因为重量属性低于标准。

我该怎么做。有没有办法。可以更改数据结构,但仅在确实需要时。

【问题讨论】:

    标签: javascript azure azure-cosmosdb


    【解决方案1】:

    我关注您的文档并在我的 cosmos db 中创建了 3 个示例文档,如下所示:

    [
      {
        "id": "1",
        "name": "My Product 1",
        "variants": [
          {
            "id": 2179,
            "code": "A",
            "attributes": {
              "att_set_1": {
                "en": {
                  "name": "Attribute Set 1",
                  "data": [
                    {
                      "id": 919,
                      "title": "Height",
                      "label": "height_mm",
                      "v": 2330,
                      "unit": "mm"
                    },
                    {
                      "id": 921,
                      "title": "Weight",
                      "label": "weight",
                      "v": 2931,
                      "unit": "kg"
                    },
                    {
                      "id": 923,
                      "title": "Other",
                      "label": "blah",
                      "v": 200,
                      "unit": "mm"
                    }
                  ]
                }
              }
            }
          }
        ]
      },
      {
        "id": "2",
        "name": "My Product 2",
        "variants": [
          {
            "id": 2180,
            "code": "B",
            "attributes": {
              "att_set_1": {
                "en": {
                  "name": "Attribute Set 1",
                  "data": [
                    {
                      "id": 919,
                      "title": "Height",
                      "label": "height_mm",
                      "v": 100,
                      "unit": "mm"
                    },
                    {
                      "id": 921,
                      "title": "Weight",
                      "label": "weight",
                      "v": 200,
                      "unit": "kg"
                    },
                    {
                      "id": 923,
                      "title": "Other",
                      "label": "blah",
                      "v": 200,
                      "unit": "mm"
                    }
                  ]
                }
              }
            }
          }
        ]
      },
      {
        "id": "3",
        "name": "My Product 3",
        "variants": [
          {
            "id": 2181,
            "code": "C",
            "attributes": {
              "att_set_1": {
                "en": {
                  "name": "Attribute Set 1",
                  "data": [
                    {
                      "id": 919,
                      "title": "Height",
                      "label": "height_mm",
                      "v": 2000,
                      "unit": "mm"
                    },
                    {
                      "id": 921,
                      "title": "Weight",
                      "label": "weight",
                      "v": 999,
                      "unit": "kg"
                    },
                    {
                      "id": 923,
                      "title": "Other",
                      "label": "blah",
                      "v": 200,
                      "unit": "mm"
                    }
                  ]
                }
              }
            }
          }
        ]
      }
    ]
    

    那我用SQL:

    SELECT all FROM all join a in all.variants join b in a.attributes.att_set_1.en.data where (b.title = 'Height' and b.v >= 2000) 或 (b.title = '体重' and b.v >= 1000)

    结果数据:

    [
      {
        "all": {
          "id": "1",
          "name": "My Product 1",
          "variants": [
            {
              "id": 2179,
              "code": "A",
              "attributes": {
                "att_set_1": {
                  "en": {
                    "name": "Attribute Set 1",
                    "data": [
                      {
                        "id": 919,
                        "title": "Height",
                        "label": "height_mm",
                        "v": 2330,
                        "unit": "mm"
                      },
                      {
                        "id": 921,
                        "title": "Weight",
                        "label": "weight",
                        "v": 2931,
                        "unit": "kg"
                      },
                      {
                        "id": 923,
                        "title": "Other",
                        "label": "blah",
                        "v": 200,
                        "unit": "mm"
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      },
      {
        "all": {
          "id": "1",
          "name": "My Product 1",
          "variants": [
            {
              "id": 2179,
              "code": "A",
              "attributes": {
                "att_set_1": {
                  "en": {
                    "name": "Attribute Set 1",
                    "data": [
                      {
                        "id": 919,
                        "title": "Height",
                        "label": "height_mm",
                        "v": 2330,
                        "unit": "mm"
                      },
                      {
                        "id": 921,
                        "title": "Weight",
                        "label": "weight",
                        "v": 2931,
                        "unit": "kg"
                      },
                      {
                        "id": 923,
                        "title": "Other",
                        "label": "blah",
                        "v": 200,
                        "unit": "mm"
                      }
                    ]
                  }
                }
              }
            }
          ]
      },
      {
        "all": {
          "id": "3",
          "name": "My Product 3",
          "variants": [
            {
              "id": 2181,
              "code": "C",
              "attributes": {
                "att_set_1": {
                  "en": {
                    "name": "Attribute Set 1",
                    "data": [
                      {
                        "id": 919,
                        "title": "Height",
                        "label": "height_mm",
                        "v": 2000,
                        "unit": "mm"
                      },
                      {
                        "id": 921,
                        "title": "Weight",
                        "label": "weight",
                        "v": 999,
                        "unit": "kg"
                      },
                      {
                        "id": 923,
                        "title": "Other",
                        "label": "blah",
                        "v": 200,
                        "unit": "mm"
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    ]
    

    请注意value 列是关键字,不能在文档中使用。因此,在我的文档中,我使用 v 将其删除。


    更新答案:

    经过几次尝试,似乎无法通过 SQL 语句直接从 Cosmos DB 中查询到你想要的结果。

    然而,当我们面对复杂的查询时,Cosmos DB 为我们提供了存储过程。如果你对存储过程不太了解,可以阅读这篇article

    请参考我创建的存储过程如下:

    function sample() {
        var collection = getContext().getCollection();
    
        var isAccepted = collection.queryDocuments(
            collection.getSelfLink(),
            'SELECT * FROM c',
            function (err, feed, options) {
                if (err) throw err;
    
                var returnArray = [];
    
                if (!feed || !feed.length){
                    getContext().getResponse().setBody('no docs found');
                } else{
                    for(var i=0;i<feed.length;i++){
                        var foundHeight = false, foundWeight=false;
                        var dataArray = feed[i].variants[0].attributes.att_set_1.en.data;
                        for(var j=0;j<dataArray.length;j++){
                          var f = dataArray[j];
                          if((f.title=='Height'&&f.v>=2000){
                            foundHeight = true; 
                          } else if(f.title=='Weight'&&f.v>=1000)){ 
                            foundWeight = true; 
                          }  
                        }
                        if(foundHeight && foundWeight) 
                           returnArray.push(feed[i]);
                    }
                }
                getContext().getResponse().setBody(JSON.stringify(returnArray));
            });
    
        if (!isAccepted) throw new Error('The query was not accepted by the server.');
    }
    

    输出结果:

    [
        {
            "id": "1",
            "name": "My Product 1",
            "variants": [
                {
                    "id": 2179,
                    "code": "A",
                    "attributes": {
                        "att_set_1": {
                            "en": {
                                "name": "Attribute Set 1",
                                "data": [
                                    {
                                        "id": 919,
                                        "title": "Height",
                                        "label": "height_mm",
                                        "v": 2330,
                                        "unit": "mm"
                                    },
                                    {
                                        "id": 921,
                                        "title": "Weight",
                                        "label": "weight",
                                        "v": 2931,
                                        "unit": "kg"
                                    },
                                    {
                                        "id": 923,
                                        "title": "Other",
                                        "label": "blah",
                                        "v": 200,
                                        "unit": "mm"
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        },
        {
            "id": "3",
            "name": "My Product 3",
            "variants": [
                {
                    "id": 2181,
                    "code": "C",
                    "attributes": {
                        "att_set_1": {
                            "en": {
                                "name": "Attribute Set 1",
                                "data": [
                                    {
                                        "id": 919,
                                        "title": "Height",
                                        "label": "height_mm",
                                        "v": 2000,
                                        "unit": "mm"
                                    },
                                    {
                                        "id": 921,
                                        "title": "Weight",
                                        "label": "weight",
                                        "v": 999,
                                        "unit": "kg"
                                    },
                                    {
                                        "id": 923,
                                        "title": "Other",
                                        "label": "blah",
                                        "v": 200,
                                        "unit": "mm"
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    ]
    

    这个结果应该是你想要的。您可以在门户网站或代码中创建它。有任何问题,请告诉我。

    希望对你有帮助。

    【讨论】:

    • 谢谢你的例子。该查询将生成身高或体重匹配的所有产品。但我需要其中一种变体与重量和高度相匹配的产品。
    • @elvir.dolic 抱歉,我不确定“其中一个变体匹配体重和身高”是什么意思。请您简化一个示例,让我帮助您找出正确的 sql 吗?
    • 我已经编辑了我最初的问题。我已经使用了您的文档并调整了这些值。我希望这能解释现在的情况。
    • @elvir.dolic 请给我一些时间来更新我的答案。
    • @elvir.dolic 你好,现在有什么进展吗?
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    相关资源
    最近更新 更多