DocumentDB SQL with ARRAY_CONTAINS

前端 未结 4 1723
眼角桃花
眼角桃花 2020-12-11 15:45

I\'m playing around on https://www.documentdb.com/sql/demo, which allows me to query against sample documents that look like:

{
  \"id\": \"19015\",
  \"desc         


        
4条回答
  •  执念已碎
    2020-12-11 16:24

    EDIT: ARRAY_CONTAINS now supports partial match as Jim Scott points out below, which I think is a better answer than this accepted one.

    You servings array only has one entry {"amount": 1, "description": "bar", "weightInGrams": 21}.

    This should work for your example with a single serving:

    SELECT root
    FROM root 
    WHERE root.servings[0].description = "bar"
    

    But it sounds like that's not what you are looking for. So, assuming you have this:

    {
      ...
      "servings": [
        {"description": "baz", ....},
        {"description": "bar", ....},
        {"description": "bejeweled", ....}
      ],
      ...
    }
    

    And you want to find the documents where one of the servings has the description "bar", then you could use this UDF:

    function(servings, description) {
        var s, _i, _len;
        for (_i = 0, _len = servings.length; _i < _len; _i++) {
            s = servings[_i];
            if (s.description === description) {
                return true;
            }
        }
        return false;
    }
    

    With this query:

    SELECT * FROM c WHERE udf.findServingsByDescription(c.servings, "bar")
    

提交回复
热议问题