1

My MongoDB knowledge is pretty compact I would say but, in my MongoDb setup we save translations in an array. The problems is that I need to sort my titles on the original language code. So I want the originalLangCode value in the sort function.

So if I would to sort it only on the NL language. My query would be like this:

db.getCollection("name").find({}).sort({"translations.NL.title" : 1});

But what I actually want is something like :

 db.getCollection("name").find({}).sort({"translations.originalLangCode.value.title" : 1});

one of the records

So guys, any help would be appreciated!

3
  • Why may I not upload images of code on SO when asking a question? Commented Oct 7, 2016 at 7:43
  • so you want to sort by whatever value "originalLangCode" will have am i right Commented Oct 7, 2016 at 9:26
  • Will therel be limited numbers of language in all the documents? Commented Oct 7, 2016 at 9:36

1 Answer 1

1

It is not a good practice to have values as keys. When you use values as key it is hard to query against them, as in mongodb you can't pass values in key field.

If you have a limited no of language code(NL,DE) you can use below code to sort them according the originalLangCode.

db.getCollection('name').aggregate(
   [
      {
         $project: {
            id_: 1,
            corLang: 1,
            corLat: 1,
            translations: 1,
            orignalsort: {
               $concat: [{
                    $cond: [ { $eq: ["$originalLangCode", "NL"] }, "$translations.NL.title", "" ]
                  },{
                    $cond: [ { $eq: ["$originalLangCode", "DE"] }, "$translations.DE.title", "" ]
                  },{
                    $cond: [ { $eq: ["$originalLangCode", "FR"] }, "$translations.FR.title", "" ]
                  }]
            }
         }
      },
      {
        $sort: {orignalsort: 1}
      }
   ]
)

**The Above solution is for mongodb version less than 3.3.5 and you should use it as latest stable release is 3.2

From upcoming version 3.3.5 we can use the $switch operator, with switch solution will be like below code**

db.getCollection('name').aggregate(
   [
      {
         $project: {
            id_: 1,
            corLang: 1,
            corLat: 1,
            translations: 1,
            orignalsort: {
                $switch: {
                    branches: [
                        {
                            case: { $eq: ["$originalLangCode", "NL"] },
                            then: "$translations.NL.title"
                        },  
                        {
                            case: { $eq: ["$originalLangCode", "DE"] },
                            then: "$translations.DE.title"
                        },  
                        {
                            case: { $eq: ["$originalLangCode", "FR"] },
                            then: "$translations.FR.title"
                        },                   
                    ],
                    default: "$translations.NL.title"
                }
            }
         }
      },
      {
        $sort: {orignalsort: 1}
      }
   ]
)

I have tested first code on my local system, but haven't checked switch case.

Sign up to request clarification or add additional context in comments.

1 Comment

Cool, you helped me a lot! Thanks for giving me ideas like that!

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.