lundi 1 juillet 2019

Sort by document array field laravel Mongodb

I'm trying to get results sorted by count of array attribute of the document, in my case each document has an array of posts count, I want to fetch sorted result based on posts count, here is the schema.

[{
        "_id" : ObjectId("4e84f78b26d2046d5d00b5b2"),
        "user_id" : 3,
        "posts" : [
                "123",
                "1234"
        ],
        "text" : "John"
},
{
        "_id" : ObjectId("4e84f78b26d2046d5d00b5b3"),
        "user_id" : 2,
        "posts" : [
                "3432",
                "2322",
                "1232"
        ],
        "text" : "James"
},
{
        "_id" : ObjectId("4e84f78b26d2046d5d00b5b4"),
        "user_id" : 3,
        "posts" : [
        ],
        "text" : "Jimmy"
}]

Can someone help to make the query that can fetch result sorted by count of posts using laravel jessenger mongodb.

Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire