DocumentDB SQL with ARRAY_CONTAINS
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")
Not sure if this functionality was available when you were looking at the API originally but the ARRAY_CONTAINS now supports an optional Boolean value at the end to provide partial match support.
SELECT root
FROM root
WHERE ARRAY_CONTAINS(root.servings, { "description": "bar" }, true)
Here is sample using the documentdb demo site that queries an array that contains multiple fields for each object stored.
SELECT *
FROM food as f
WHERE ARRAY_CONTAINS(f.servings, {"description":"bar"}, true)
Note that this is a workaround that works fine.
SELECT c.id FROM c JOIN a in c.companies where a.id = '{id}'
In Linq this would be
x => x.companies.Any(z => z.id == id)