json-query-language icon indicating copy to clipboard operation
json-query-language copied to clipboard

Support matching array elements

Open clue opened this issue 11 years ago • 1 comments

Some systems do this automatically if the left hand operand is an array.

MongoDB has an explicit $elemMatch operator.

This needs some discussion first.

clue avatar Oct 22 '14 12:10 clue

What do you think of expanding the syntax to support [] in keys? Let's lay out an example

// Object 1
{
  "x": [
    ["a", "b"],
    ["c", "d"]
  ]
}
// Object 2
{
  "x": [
    ["a", "c"],
    ["b", "d"]
  ]
}
// Object 3
{
  "x": [
    ["a"],
    ["b"]
  ]
}
// Object 4
{
  "x": [
    {"e": "f"}, 
    "a"
  ]
}
// Object 5
{
  "x": [
    "apple"
  ]
}
// Object 6
{
  "x": [
    {"a": "b"}
  ]
}

Currently, the syntax supports

{ "x": { "$contains": ["a", "b"] } } => Object 1

Which is not very expressive. What if you wanted to query on all objects such that an item of "x" contains "a"? It is not possible.

The new syntax proposal would let you specify a query on the inner lists, while maintaining all existing properties of $contains. For example,

{ "x[]": { "$contains": "a" } } => Object 1, 2, 3, 4, 5, 6

In the case of explicit dictionary matching, you can combine the dot syntax with the bracket syntax as in

{ "x[].a": { "$contains": "b" } } => Object 6

To maintain consistency with the existing comparators we would also need to define semantics when this proposed keying syntax is combined with $in

{ "x[]": { "$in": ["a"] } } => Object 4

Which should be noted is surjective to the upper level "contains"

{ "x": { "$contains": ["a"] } } => Object 4

Furthermore, we can express arbitrary list levels

{ "x[][]": { "$in": ["a"] } } => Object 1, 2, 3

Similarly to how a nested key will simply return nothing if no object is nested in the specified way, at any point, a further list can be expressed without violating the query syntax but will silently match to nothing

{ "x[][][]": { "$in": ["a"] } } => EMPTYSET

Of course, this means that we would have to special case the [] syntax and introduce a similar system as for the \\. notation. So a literal [ or ] character in a key would be expressed by \\[ or \\], respectively.

Thoughts?

mnm364 avatar Mar 14 '18 00:03 mnm364