In native queries it is possible to query a document based on the contents of an array field. Take a document that has a field like
{
"_id": ...,
"otherFields": ...,
"positions": [
{
"x": 1,
"y": 15
},
{
"x": 3,
"y": 15
},
{
"x": 3,
"y": 9
}
]
}
In native queries you could do a $match
like the following to retrieve all documents where an entry in positions
has x
of 3
:
{
$match: {
positions: {
$elemMatch: {"x": 3}
}
}
}
Is a similar per-array-element query possible with Atlas $search
syntax?
If the positions
sub-documents were in their own collection I know you could do a $search
like
{
$search: {
range: {
path: 'x',
gte: 3,
lte: 3
}
}
}
but this query doesn’t work on nested fields of an array (and I’m not here to ask about re-structuring the data). Also, due to the nature of $search
I can’t first do an $unwind
, and doing a $match
after the $search
would be a major hit to query speed.