0

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

chiku
  • 443
  • 1
  • 5
  • 17
  • I'm going to refer you there again since your question lacks any attempt at writing the code yourself. I'm also pointing you to the ["double nested array" existing answer.](https://stackoverflow.com/questions/29071748/find-in-double-nested-array-mongodb) – Neil Lunn Nov 13 '17 at 10:40
  • @NeilLunn the example was single array of elements, but in my case Its array into an array of elements and want to find out the element in the sub sub document array. the answer does not works out for me, /Please help. Kris – chiku Nov 13 '17 at 10:44
  • Really? I wonder what the answer on the other question you are linked to ( and also in the comment just above ) actually says then? The 3 minute timeframe tells me you have not read it. – Neil Lunn Nov 13 '17 at 10:46
  • @NeilLunn I have modified the code but was yielding zero results. Please help. kris – chiku Nov 13 '17 at 12:03
  • Thanks @NeilLunn, it worked cool, – chiku Nov 20 '17 at 07:38
  • @NeilLunn : can u please give me a solution for https://stackoverflow.com/questions/47388697/slice-to-find-out-the-max-element-in-deep-nested-array-in-mongodb . Regards, Kris – chiku Nov 20 '17 at 15:10

0 Answers0