Use boolean operators in $elemMatch on array of non-object? Any way to refer to the individual element?

Is it possible to use (explicit) boolean operators inside an $elemMatch query on an array of non-object elements?

    { xs: [1, 2, 3] },
    { ys: [{'y': 1}, {'y': 2}, {'y': 3}] },

db.test.find({xs: {$elemMatch:
    {$gt: 1, $lt: 7}

// If we want to use boolean operators, we end up in a context similar to
// a document query where we need to refer to a field name. As a
// workaround, we can require the elements to be objects with a dummy
// field name.
db.test.find({ys: {$elemMatch:
    {$and: [{y: {$gt: 1}}, {y: {$lt: 7}}]}

// But is it possible to use boolean operators in $elemMatch without
// requiring the elements to be objects? Is there any way to refer to an
// individual element of xs being queried?
db.test.find({xs: {$elemMatch:
    {$and: [{?: {$gt: 1}}, {?: {$lt: 7}}]}

It feels odd that the limitation is a matter of syntax – we simply don’t have a name to refer to an individual element in question. The $filter aggregation operator addresses this problem with its as option which allows us to give a name to the element. Does there exist anything similar for $elemMatch?


Not that I can think of. But what kind of operation does not work with implicit and like:

The thought originally arose because I started sketching out an idea involving dynamically-generated queries, so I don’t have an illustrative and compelling example because the operation is (somewhat) arbitrary and not known ahead of time.