Mongodb 具有两个$OrderBy的复杂查询

Mongodb 具有两个$OrderBy的复杂查询,mongodb,bson,mongodb-c,Mongodb,Bson,Mongodb C,以下是我收藏的结构部分: by: [ { id: ObjectId("XX"), type: NumberInt(1) } ], timestamp: NumberInt(), // is timestamp 1 status: NumberInt(0), mails: [ { id: ObjectId("YY"), text: "", timestamp: NumberInt(), // is timestamp

以下是我收藏的结构部分:

by: [
   {
      id: ObjectId("XX"),
      type: NumberInt(1)
   } 
],
timestamp: NumberInt(), // is timestamp 1
status: NumberInt(0),
mails: [
   {
      id: ObjectId("YY"),
      text: "",
      timestamp: NumberInt(), // is timestamp 2
   }
]
我可以通过以下几行按照时间戳1按时间顺序恢复数据:

...
bson_init(&query);
bson_append_document_begin(&query, "$orderby", -1, &child);
bson_append_int32(&child, "timestamp", -1, 1);
bson_append_document_end(&query, &child);
bson_append_document_begin(&query, "$query", -1, &child);
bson_append_document_end(&query, &child);
collection = mongoc_client_get_collection(client, "db", "prefix");
cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, NULL, NULL);
while(mongoc_cursor_next(cursor, &doc)){
   bson_iter_t iter;
   if(bson_iter_init_find(&iter, doc, "status")){
      status = bson_iter_int32(&iter);
   }
   ...
}
...

但现在我想按时间顺序(或不按时间顺序)检索数组“mails”中的所有值。。。你知道这个过程吗?

看起来你必须使用它才能让它工作

这个想法(在javascript中):


我现在不知道如何把它翻译成C。尝试签出文档。

顺便说一句,看起来您是在按
文件名
输入代码进行排序。您的示例文档中没有此密钥。抱歉,这是我的错。这是时间戳键,不是文件名。没关系。顺便说一句,看看我的答案,也许它会帮助你。
db.test.aggregate([
    { $unwind: "$mails" }, 
    { $sort: { timestamp: 1, "mails.timestamp": 1 } }, 
    { $group: { _id: "$_id", mails: { $push: "$mails" } } }
]);