No. $nodes.0
no es una expresión correcta en $eq aggregation operator
, cuando se usa con campos de matriz.
$eq aggregation operator
tiene la siguiente snytax:
{ $eq: [ <expression1>, <expression2> ] }
Tendrás que usar $reduce
junto con $arrayElemAt
para acceder a su campo en el operador $eq:
Consulta1
db.relations.find({
$expr: {
$eq: [
{
$reduce: {
input: [
{
$arrayElemAt: [
"$nodes",
0
]
}
],
initialValue: false,
in: {
"$and": [
{
$eq: [
"$$this.type",
"User"
]
},
{
$eq: [
"$$this.id",
UUID("dc20f7c7-bd45-4fc1-9eb4-3604428fa551")
]
}
]
}
}
},
true
]
}
})
Alternativa, usando el operador $eq { <field>: { $eq: <value> } }
Consulta2
db.relations.find({
"$and": [
{
"nodes.0.type": {
$eq: "User"
}
},
{
"nodes.0.id": {
$eq: UUID("dc20f7c7-bd45-4fc1-9eb4-3604428fa551")
}
}
]
})
Si desea realizar IXSCAN, puede utilizar Query2 . Tienes que crear los siguientes índices:
db.relations.ensureIndex({"nodes.0.id":1})
db.relations.ensureIndex({"nodes.0.type":1})