Javascript Azure CosmosDB对子元素的查询

Javascript Azure CosmosDB对子元素的查询,javascript,azure,azure-cosmosdb,Javascript,Azure,Azure Cosmosdb,我有这样的文件 [ { "id": "1", "name": "My Product 1", "variants": [ { "id": 2179, "code": "A", "attributes": { "att_set_1": { "en": { "name": "Attribute

我有这样的文件

   [
    {
      "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不匹配,因为重量属性低于标准


我怎样才能做到这一点。有办法吗。数据结构可以更改,但只有在真正需要时才可以更改

我遵循您的文档,在我的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:

从所有中选择所有在所有中加入a。在所有中加入b a、 attributes.att_set_1.en.数据,其中(b.title=‘Height’和b.v>= 2000)或(b.title=重量,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"
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
]
请注意,
列是一个关键字,不能在文档中使用。所以,在我的文档中,我用v删除它


更新答案:

经过几次尝试之后,似乎不可能通过SQL语句直接从Cosmos DB查询您想要的结果

然而,当我们面对复杂的查询时,COSMOSDB为我们提供了存储过程。如果您对存储过程了解不多,可以阅读以下内容

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

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.');
}
这个结果应该是你想要的。您可以在门户上或在代码中创建它。如果有任何问题,请告诉我


希望对您有所帮助。

谢谢您的示例。该查询将生成高度或重量匹配的所有产品。但我需要其中一种变体与重量和高度匹配的产品。@elvir.dolic抱歉,我不确定这里“其中一种变体与重量和高度匹配”的含义是什么。请您简化一个示例,让我帮助您找出正确的sql,好吗?我已经编辑了我的初始问题。我已经使用了您的文档并修改了值。我希望这能解释这个问题。@elvir.dolic请给我一些时间更新我的答案。@elvir.dolic嗨,现在有什么进展吗?
[
    {
        "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"
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
]