0

I have a schema like this

{
    _id:ObjectId(),
    ...,
    translations:[{
        value: "English",
        code:"en"
    },{
        value: "German",
        code:"de"
    }]
}

All object have a translation with code 'en' and 'de', How can I get all objects(value,id) with code 'en'? A The result should look like similar to this:

[{
    _id:ObjectId(),
    value:"English"
},....
...]
1

1 Answer 1

2

You can do this using aggregation pipeline

  • $unwind the translations array
  • Use $match to select the documents with code 'en'
  • Use $project to include or reset fields in your result.
    db.collection.aggregate(
        [
            { "$unwind": "$translations"}, 
            { "$match": { "translations.code": "en" }}, 
            { "$project": { "value": "$translations.value", "_id": 1 }}
       ]
    )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.