I have this MongoDB collection:
{ "_id" : ObjectId("123"), "from_name" : "name", "from_email" : "email@mxxxx.com", "to" : [ { "name" : "domains", "email" : "domains@xxx.com" } ], "cc" : [ ], "subject" : "mysubject" }
My goal is to search in this collection by the "to" with some email.
If you only want one field then MongoDB has "dot notation" for accessing nested elements:
And this will return documents that match:
For more that one field as a condition, use the
$elemMatch
operatorAnd you can "project" a single match to just return that element:
But if you expect more than one element to match, then you use the aggregation framework:
All fun ways to match on and/or "filter" the content of an array for matches if required.