Order of $sort and $limit aggregation

Try

db.documents.aggregate([
  {
    "$match": {
      "userId": 1
    }
  },
  {
    "$sort": {
      updatedAt: -1
    }
  },
  {
    "$skip": 0
  },
  {
    "$limit": 10
  }
  {
    "$lookup": {
      "from": "html",
      "localField": "docId",
      "foreignField": "_id",
      "as": "content"
    }
  },
  {
    "$lookup": {
      "from": "resources",
      "localField": "resources",
      "foreignField": "_id",
      "as": "resources"
    }
  }
])

with index

{ userId : 1 , updatedAt : -1 }

1 Like