0

With this dataset, I’m trying to do a query which return for all the document, the rank of the articles relevant. But I don’t know if it is possible with only a mongo query.

content_business : {
id : {….} ,
content : {
    uid : « 01234 »,
    FL : "bla",
    langRef : 1,
    articles : [ 
        {
            name : « aName »,
            rank : 104
        },
        {
            name : « unNom »,
            rank : 102
        }
    ]
}
}

A content contains a langRef. This is the index to use to get the right article. Here with the value 1 it means that the articles which is relevant is the one with the index 1 { name : « unNom », rank : 102 }.

For the moment I do a db.find({« FL : bla »}), then with an external program I’m getting the rank of the related (sort of articles[langRef].rank)

But not sure it is the better solution.

Have you got any idea ?

Regards,

Blured.

Blured Derulb
  • 201
  • 3
  • 12
  • You could do it with an aggregation, but sorting on the application level is likely the easier way to do it. – Philipp Sep 17 '15 at 09:02
  • Possible duplicate of [how to sort the field in the mongo document which is inside array](http://stackoverflow.com/questions/32605984/how-to-sort-the-field-in-the-mongo-document-which-is-inside-array/) – Philipp Sep 17 '15 at 09:02

1 Answers1

0

It is not really a sort as far as I understand :

What I have got in entry :

content_business : {
    id : {….} ,
    content : {
        uid : « 01234 »,
        FL : "bla",
        langRef : 1,
        articles : [ 
            {
                name : « aName »,
                rank : 104
            },
            {
                name : « unNom »,
                rank : 102
            }
        ]
    }
},
{
    id : {….} ,
    content : {
        uid : « 99999 »,
        FL : "bla",
        langRef : 0,
        articles : [ 
            {
                name : « aaaa »,
                rank : 888
            },
            {
                name : « bbbb »,
                rank : 102
            }
        ]
    }
}

What I'd like in result

{
    id : {...},
    content : { name : "unNom", rank : 102}
},
{
    id : {...},
    content : {name : "aaa", rank : 888}
}

The first document give the name unNom & rank 102 as the specified langRef is 1

the second document give the name "aaa" & rank 888 as the specified langRef is 0

Regards, Blured.

Blured Derulb
  • 201
  • 3
  • 12