0

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?

Volodymyr Zh
  • 317
  • 7
  • 19

1 Answers1

0

$sort is deprecated. You need to use sort function with find.

db.tasks.find({_id : "590e2d7d4d4a5e0b647d0835" })
.sort({context_task.created_task : 1})
.limit(1)
hurricane
  • 6,521
  • 2
  • 34
  • 44
  • Thank, but it doesn't work for me. I am using next query and this working. `db.tasks.aggregate({$match: { "_id":ObjectId("592e87cf7a91b810209235cc")}},{ $unwind: '$context_task' },{$sort: {'context_task.created_task': -1}}).pretty();` – Volodymyr Zh May 31 '17 at 13:45