I referred to this link Retrieve only the queried element in an object array in MongoDB collection but the example was retrieve an element from an object array in mongodb. Now I want to retrieve an element from array of an array, please help me how to do the same.
Example records :
 {  
    "_id":ObjectId("562e7c594c12942f08fe4192"),
   "shapes":[  
  {  
     "shape":"square",
     "color":"blue",
     "fileArray:[
        {"fileid" : 'ID001',"filename" : "Abc.jpg", filepath" :"d:/temp/"},
        {"fileid" : 'ID002',"filename" : "Cde.jpg", filepath" :"d:/temp2/"}
      ]
  },
  {  
     "shape":"circle",
     "color":"red",
     "fileArray:[
        {"fileid" : 'ID003',"filename" : "Abc.jpg", filepath" :"d:/temp/"},
        {"fileid" : 'ID004',"filename" : "Cde.jpg", filepath" :"d:/temp2/"}
      ]
  }
   ]
 },
  {  
     "_id":ObjectId("562e7c594c12942f08fe4193"),
     "shapes":[  
  {  
     "shape":"square",
     "color":"black",
     "fileArray:[
        {"fileid" : 'ID001',"filename" : "Abc.jpg", filepath" :"d:/temp/"},
        {"fileid" : 'ID002',"filename" : "Cde.jpg", filepath" :"d:/temp2/"}
      ]
  },
  {  
     "shape":"circle",
     "color":"green",
     "fileArray:[
        {"fileid" : 'ID003',"filename" : "Abc.jpg", filepath" :"d:/temp/"},
        {"fileid" : 'ID004',"filename" : "Cde.jpg", filepath" :"d:/temp2/"}
      ]
  }
    ]
       }
My requirement is to find out filepath and filename when fileid and shape.color is red.
As Niell suggested the link Find in Double Nested Array MongoDB:
    db.dummies.aggregate([
       { "$match": {
"shapes": {
  "$elemMatch": {
     "color": "blue",
     "fileArray": {
       "$elemMatch": {
         "fileid": "ID001"//,
         //"otherField": 1
       }
     }
   }
}
      }}
     ,
       { "$addFields": {
"shapes": {
  "$filter": {
    "input": {
      "$map": {
        "input": "$shapes",
        "as": "sa",
        "in": {
         // "name": "$$sa.name",
          "fileArray": {
            "$filter": {
              "input": "$$sa.fileArray",
              "as": "sn",
              "cond": {
                "$and": [
                  { "$eq": [ "$$sn.fileid", "ID001" ] }//,
                 // { "$eq": [ "$$sn.otherField", 1 ] }
                ]
              }
            }
          }             
        }
      },
    },
    "as": "sa",
    "cond": {
      "$and": [
        { "$eq": [ "$$sa.color", "blue" ] }
        //,{ "$gt": [ { "$size": "$$sa.someNestedArray" }, 0 ] }
      ]
    }
  }
      }
      }}
   ])
This query returns zero results :
       {
        "_id" : ObjectId("562e7c594c12942f08fe4192"),
          "shapes" : [] 
           }
Please lemme know where I am going wrong Regards
kris
