Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何对mongodb/mongoose中的嵌套字段进行排序_Mongodb_Mongoose Schema - Fatal编程技术网

如何对mongodb/mongoose中的嵌套字段进行排序

如何对mongodb/mongoose中的嵌套字段进行排序,mongodb,mongoose-schema,Mongodb,Mongoose Schema,我试图按日期对评论进行排序,但我做不到 { "_id": "5defc10b8e753623b4ad0adf", "title": "bla bla", "owner:"idToPopulate", "comments": [ { "_id": "5dfc2185e62103121cfc0f18", "reply": "1",

我试图按日期对评论进行排序,但我做不到

   {
        "_id": "5defc10b8e753623b4ad0adf",
        "title": "bla bla",
        "owner:"idToPopulate",
        "comments": [
            {
                "_id": "5dfc2185e62103121cfc0f18",
                "reply": "1",
                "replyDate": "2019-12-10T16:00:11.228Z"
            },
            {
                "_id": "5dfc218be62103121cfc0f19",
                "reply": "2",
                "replyDate": "2019-13-10T16:00:11.228Z"
            }
        ]
    }
这是我尝试过的方法(结果必须是,按最后日期对评论排序)

使用可以对数据进行排序,但在使用之前必须拆分数组。尝试此mongo查询:

 db.collection.aggregate([
  {
    $unwind: {
      path: "$comments"
    }
  },
  {
    $sort: {
      "comments.replyDate": -1
    }
  },
  {
    $group: {
      _id: "$_id",
      comments: {
        $push: {
          _id: "$comments._id",
          item: "$comments.reply",
          date: "$comments.replyDate"
        }
      }
    }
  }
])

为什么不试试“1”而不是“-1”?我认为
-1
意味着升序(这对于1和2来说是正确的)hi@CeliusStingher我的错误(键入错误),但它不起作用,因为您必须使用聚合来对数组中的子文档进行排序-按子文档的字段排序。在这篇文章中看到聚合的答案:。很抱歉,它不起作用,我的意思是,它无法检索结果(甚至忽略了服务器响应)@AlexHunter根据您编辑的集合数据更新了查询现在它可以工作了!,在该对象中的最后一件事,在comments对象数组之外,我有更多的信息,我如何显示它呢?只需将$group stage中的值分组为:{group id:{group id:$\id],example_data:$example_data}和add one one stage$项目,以收集所有必需的值。我尝试了这种方式,结果显示,它改变了对象的顺序,我用数据更新了我的帖子,还有一些字段也需要填充
 db.collection.aggregate([
  {
    $unwind: {
      path: "$comments"
    }
  },
  {
    $sort: {
      "comments.replyDate": -1
    }
  },
  {
    $group: {
      _id: "$_id",
      comments: {
        $push: {
          _id: "$comments._id",
          item: "$comments.reply",
          date: "$comments.replyDate"
        }
      }
    }
  }
])