Get specific array element based on condition and only get this element not the whole document - mongoDB.

PHOTO EMBED

Thu Aug 11 2022 07:30:16 GMT+0000 (UTC)

Saved by @ali_alaraby

{  
   "_id":ObjectId("562e7c594c12942f08fe4192"),
   "shapes":[  
      {  
         "shape":"square",
         "color":"blue"
      },
      {  
         "shape":"circle",
         "color":"red"
      }
   ]
},
{  
   "_id":ObjectId("562e7c594c12942f08fe4193"),
   "shapes":[  
      {  
         "shape":"square",
         "color":"black"
      },
      {  
         "shape":"circle",
         "color":"green"
      }
   ]
}

db.test.find({"shapes.color": "red"}, {_id: 0, 'shapes.$': 1});
content_copyCOPY