Title: MongoDb projection on array of objects, Get matched object only
Tags: arrays;mongodb;mongodb-query
Question: I have mongo document like this

```{
    "_id": "5b14679e592baa493e0bc208",
    "productCode": "ABC",
    "corridors": [
        {
            "countryNameEn": "Sweden",
            "countryNameFr": "Suède",
            "countryCode": "SE",
            "currencyNameEn": "Swedish Krona",
            "currencyNameFr": "Couronne suédoise",
            "currencyCode": "SEK",
            "corridorLimit": "abc"
        },
        {
            "countryNameEn": "USA",
            "countryNameFr": "Suède",
            "countryCode": "US",
            "currencyNameEn": "USA",
            "currencyNameFr": "Couronne suédoise",
            "currencyCode": "USD",
            "corridorLimit": "abc"
        }
    ]
},
{
    "_id": "5b14679e592baa493e0bc208",
    "productCode": "XYZ",
    "corridors": [
        {
            "countryNameEn": "Sweden",
            "countryNameFr": "Suède",
            "countryCode": "SE",
            "currencyNameEn": "Swedish Krona",
            "currencyNameFr": "Couronne suédoise",
            "currencyCode": "SEK",
            "corridorLimit": "abc"
        },
        {
            "countryNameEn": "USA",
            "countryNameFr": "Suède",
            "countryCode": "US",
            "currencyNameEn": "USA",
            "currencyNameFr": "Couronne suédoise",
            "currencyCode": "USD",
            "corridorLimit": "abc"
        }
    ]
}
```

I want to find document whose ```productCode``` is ```ABC``` and ```currencyCode``` is ```USD``` in corridors array. How can I return only the matching object from ```corridors``` array like only only object that has ```USD``` ```currencyCode``` should come in result and not all array.

I tried running this query ```{ productCode: 'ABC', corridors: { $elemMatch: { currencyCode: "USD"}}}```. But it gives me the whole array and not the only matched element.

Comment: refer to this documentation. use $elemmatch in projection https://docs.mongodb.com/manual/reference/operator/projection/elemMatch/

Comment: use it in projection

Comment: I am already using $elemmatch


Here is another answer: I don't think you can use $elemMatch in $project. Check this

Try the following query: 

```db.collection.aggregate([
{

    $match : {"productCode" : "ABC"}
}, 
{
    $unwind : "$corridors"
}, 
{
    $match : { "corridors.currencyCode" : "USD"}
}, 
{
    $group : {
              _id : "$productCode", 
              corridors : {$addToSet : "$corridors"}
}
}]);
```

Outputs: 

```{
    "_id" : "ABC",
    "corridors" : [ 
        {
            "countryNameEn" : "USA",
            "countryNameFr" : "Suède",
            "countryCode" : "US",
            "currencyNameEn" : "USA",
            "currencyNameFr" : "Couronne suédoise",
            "currencyCode" : "USD",
            "corridorLimit" : "abc"
        }
    ]
}
```

In the result you'll have ```_id``` instead of ```productCode```. If you still want ```productCode```, you can just include ```$project``` in the end. 

Hope this helps!

Comment for this answer: You "think" wrong then. The [`$elemMatch`](https://docs.mongodb.com/manual/reference/operator/projection/elemMatch/) operator has two forms, being one for query and the other ( linked one ) for projection. The question has been long answered though and it's preferable to use `$filter` for "multiple matches" than `$unwind` anyway, for a whole host of reasons. But a standard `$elemMatch` or [positional `$` operator](https://docs.mongodb.com/manual/reference/operator/projection/positional/) is all that is needed for a "single" matching result.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:06/13/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<arrays><mongodb><mongodb-query>', 'Title': 'MongoDb projection on array of objects, Get matched object only', 'ViewCount': 1065, 'id': 50845683, 'user_AboutMe': '<p>A result-driven, articulate and analytical Computer programmer with over 5+ years of professional work experience in various front-end technologies and back-end technologies. Capable of writing pixel perfect web applications with strong expertise in JavaScript and JavaScript MV* frameworks like AngularJS, ReactJS, BackboneJS, EmberJS. Experience of working in NodeJS, ExpressJS, HapiJS, MongoDB. Experience of writing micro services using NodeJs and using application architectures REDUX, FLUX and templating engines. Fluent in writing cross browser HTML, CSS and hybrid applications.</p>\n', 'user_AccountId': 8227721.0, 'user_DisplayName': 'Waqar Azhar', 'user_Reputation': 64.0, 'user_Views': 25.0, 'user_id': 6189479.0}}