Wednesday, 31 May 2017

How sort array of objects in collection MongoDB?

I am using node.js and MongoDB. I have collection with next schema:

var project = new Schema({
head:{
    head_task: String,
    userID: String
},
access_users: {
    type : Array ,
    "default" : []},
context_task: [{
            from_name: String,
            status: Boolean,
            task: String,
            created_task: {
                type: Date,
                default: Date.now()
             }
        }],
created: {
        type: Date,
        default: Date.now
    }});

How I can get docs sorting by field context_task.created_task by DESC. I think that I have to using aggregate query

db.tasks.aggregate({$match :{"_id":ObjectId("590e2d7d4d4a5e0b647d0835")}},
 {$sort: {"context_task.created_task": -1}}).pretty();

but sorting doesn't work. Maybe I have to sort Data on client? Can anyone help me with this question?



via Вова Журавлёв

No comments:

Post a Comment