mongodb - Retrieving object from an array using '$filter' along with all other fields -


in product catalog there items 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 $elemmatch i'm not sure if that's i'm looking for.

i select whole item localized strings in description.

i've tried no success:

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

it returns whole item both languages in description.

also i'm wondering happen items don't have selected language localization (should possible select 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 returns description, not other fields of items collection.

to extend question, know if it's possible select 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 select whole item texts in localized language.

is possible? if not, how resolved? (i'm thinking maybe in separating localized subdocuments , populating them after selecting, or maybe map-reduce function? i'm wondering how impact in performance).

i've been looking in different articles , confusing: seems there not consensus topic.

some of them opt separate collection translations (wich seems makes difficult maintain deleted texts), others selecting whole texts , filtering them (which seems bad option when there multiple languages: lot of post-processing), or sending them client (which seems inefficient send amount of unused data).


Comments