0

In my product catalog there are items like this one:

[
    {
        "title": "A great Item",
        "ltitle": "a great item",
        "brand": {
            "name": "MyBrand"
        },
        "description": [
            {
                "lang": "en-en",
                "full": "<p>Super great item bla bla bla super great bla bla</p>",
                "short": "Super great item..."
            },
            {
                "lang": "es-es",
                "full": "<p>Producto de muy alta calidad bla bla bla alta calidad etc</p>",
                "short": "Producto de muy..."
            }
        ]
    },
    ...
]

I've been reading about $elemMatch but I'm not sure if that's what I'm looking for.

I would like to select the whole item but only localized strings in description.

I've tried with no success:

db.items.find({description: { $elemMatch: { lang: "en-en" } } })

It returns the whole item with both languages in description.

Also I'm wondering what would happen with items that don't have the selected language localization (should be possible to select a default language).

Also tried:

db.items.find({ "description.lang": "en-en"}, { _id:0, description: { $elemMatch: { lang: 'en-en' } } })

And:

db.items.aggregate([
    { $match: { 'description.lang': 'en-en' } },
    { $project: {
        description: {
            $filter: {
                input: '$description',
                as: 'desc',
                cond: { $eq: [ '$$desc.lang', 'en-en'] }
            }
        },
        _id:0
    } }
])

But both of them just returns description, not the other fields of items collection.

To extend my question, I would like to also know if it's possible to select only localized text in multiple fields:

[
    {
        "title": [
            {
                "lang": "en-en",
                "title": "A great Item"
            },
            {
                "lang": "es-es",
                "title": "Un gran producto"
            },
        ],
        "description": [
                {
                    "lang": "en-en",
                    "full": "<p>Super great item bla bla bla super great bla bla</p>",
                    "short": "Super great item..."
                },
                {
                    "lang": "es-es",
                    "full": "<p>Producto de muy alta calidad bla bla bla alta calidad etc</p>",
                    "short": "Producto de muy..."
                }
        ]
    }
]

I would like to select the whole item but with texts in localized language.

Is it possible? If not, how is this resolved? (I'm thinking maybe in separating localized subdocuments and populating them after selecting, or maybe a map-reduce function? Also I'm wondering how will this impact in performance).

I've been looking in different articles and it is confusing: it seems there is not really consensus about this topic.

Some of them opt for a separate collection with translations (wich seems that makes difficult to maintain deleted texts), others by selecting the whole texts and filtering them (which seems a bad option when there are multiple languages: a lot of post-processing), or even sending them to the client (which seems inefficient to send that amount of unused data).

Shrabanee
  • 2,506
  • 1
  • 16
  • 26
Miquel
  • 7,052
  • 6
  • 49
  • 75
  • Please look into the [answer](http://stackoverflow.com/a/37207685/4273915). Which will help you I think. – Shrabanee Sep 02 '16 at 10:12
  • 1
    Yes, I've tried with `$filter` and I found that it works, but its needed to specify all fields you don't want to filter in `$filter`. This could be a no-no in a variable document fields, and make it difficult to maintain when you're developping, as new fields may be added. It's a possibility but if it would exist a *don't filter any field except `description`* – Miquel Sep 02 '16 at 10:18
  • Sorry buddy! This is how you have to use. No other option. For that only aggregation is not so reliable to use. – Shrabanee Sep 02 '16 at 10:21
  • 1
    I've found this [link](https://docs.mongodb.com/manual/reference/operator/aggregation/group/#group-documents-by-author) referring to `$group` with `$$ROOT` and it seems promising. Also have to say that working with mongoose it can be an option if the model is read and populated in `$filter`... Will think about it... – Miquel Sep 02 '16 at 10:23
  • 1
    Cool!! Post the solution here, if you could make it work. Will be helpful for others. – Shrabanee Sep 02 '16 at 10:24

0 Answers0