Order result by value

Hi all.

I have here a structure of the document:

{
   "type": "fileActivity",
   "docId": "001c49f1e3e3",
   "createdAt": 1408666740,
   "updatedAt": 1408666740,
   "file": "f23c6aa8f5e4a83c",
   "fromUserId": "a056833fe94a",
   "toUserId": "a056833fe94a",
   "subject": "systest",
   "message": "testtest",
   "fromUserName": "mne@sergeykozlov.ru",
   "toUserName": "mne@sergeykozlov.ru"
}

Also I have a view here:

function (doc, meta) {
  if (meta.type == "json" && doc.type == "fileActivity") {
    // Check if doc is JSON
    emit(doc.toUserId, {
      'docId': doc.docId,
      'createdAt': doc.createdAt,
      'updatedAt': doc.updatedAt,
      'file': doc.file,
      'subject': doc.subject,
      'message': doc.message,
      'fromUserName': doc.fromUserName,
      'toUserName': doc.toUserName,
      'read': doc.read
    });
  } else {
    // do something with binary value
  }
}

And here I get the following result:

"id":"f9842b995be9", "value": {
"docId":"f9842b995be9",
"createdAt":1417133905,
"updatedAt":1417133905,
"file":"f23c6aa8f5e4a83c",
"subject":"systest",
"message":"testtest",
"fromUserName":"mne@sergeykozlov.ru",
"toUserName":"mne@sergeykozlov.ru"}

Can I sort the result on the field createdAt?

I do it like this:

$query = CouchbaseViewQuery::from("fileActivity", 'toUserId')->key($cbMy['userId'])->order(CouchbaseViewQuery::ORDER_DESCENDING)->stale(CouchbaseViewQuery::UPDATE_BEFORE);

You have to use createdAt as a key, when you are emitting values

You mean, change view like this?:

function (doc, meta) {
  if (meta.type == "json" && doc.type == "fileActivity") {
    // Check if doc is JSON
    emit(doc.createdAt, {
      'docId': doc.docId,
      'createdAt': doc.createdAt,
      'updatedAt': doc.updatedAt,
      'file': doc.file,
      'subject': doc.subject,
      'message': doc.message,
      'fromUserName': doc.fromUserName,
      'toUserName': doc.toUserName,
      'read': doc.read
    });
  } else {
    // do something with binary value
  }
}

but then I can only search by createdAt, and I need to look across the field toUserId and sort by createdAt.