Reputation: 8989
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 (which 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).
Upvotes: 0
Views: 133