Tengo la siguiente estructura de documento en MongoDB.
[ { "network_type": "ex", "rack": [ { "xxxx": { "asn": 111111, "nodes": { "business": [ "tt550abcc1eb01" ], "master": [ "tt550abcc1eb02" ] }, "region": "ex-01", "zone": "01a" }, "yyyy": { "asn": 22222, "nodes": { "business": [ "er44abcc1eb28" ] }, "region": "ex-02", "zone": "02a" } } ] } ]Los nombres dentro de la matriz de nodos de negocio, maestro son únicos. ¿Es posible encontrar los detalles de la matriz de nodos simplemente pasando el valor "er44abcc1eb28"? Intenté la siguiente consulta pero no funcionó. Por favor ayuda.
db.collection.find({"rack.$[].nodes.$[]": "er44abcc1eb28"},{}) db.collection.aggregate([ { //Reshape the array "$unwind": "$rack" }, { "$project": { "racks": { //To handle dynamic key ie xxxx, yyyy "$objectToArray": "$rack" } } }, {. //Reshape to match the element $unwind: "$racks" }, { "$match": { //match condition "racks.v.nodes.business": "er44abcc1eb28" } }, { $group: { //Group the results incase there are more matches per id "_id": "$_id", data: { $push: "$racks" } } }, { "$project": { "rack": { //Reshape the data to the original form "$arrayToObject": "$data" } } } ]) //To understand, remove each stage and check, refer the documentation.