Aggregate variable as field name in project

Hello to all!

I have next project with aggregation:

.project({

                '_id': 0,

                'productId': 1,

                'productName.fr' : 1,

                'size': 1,

                'brand': 1,


})

Question: how to set variable as filedName? Something like this?

.project({

                '_id': 0,

                'productId': 1,

                `productName.*${lang}*` : 1,

                'size': 1,

                'brand': 1,


})

Any help is appreciated.

Hi @Il_Chi ,

Why can’t you achieve it on application side while building the pipeline and before sending it to the server?

Is this variable come from one of the stages in the pipeline?

Thanks
Pavel

2 Likes

So, with your idea I will have 4 “projects” in my code for 4 languages

or

will need to have aggregation query always

Hi @Il_Chi ,

Not sure I understand the question. I thought the projection is done based on a user specific input from the app. For example if a user chose “eng” language , you will project the relevant english field for product object, when the user changes it you will re-query with a different projection.

On the other hand if your documents look like the following:

{
...
product : {
 eng : "...", 
 fr : "..." ,
...
}
}

You can just project product : 1 and in your application access the relevant field for the language you choose.

doc.product[`${lang}`] 

Hope this shade some light…

Pavel

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.