Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.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
Node.js 如何在下面显示的聚合查询中添加排序、限制和跳过?_Node.js_Database_Mongodb - Fatal编程技术网

Node.js 如何在下面显示的聚合查询中添加排序、限制和跳过?

Node.js 如何在下面显示的聚合查询中添加排序、限制和跳过?,node.js,database,mongodb,Node.js,Database,Mongodb,这是我的数据结构 { "_id" : ObjectId("57f37f18517f72bc09ee7632"), "name" : "testdata", "createdBy" : "57f1fdef1d3c40141617d215", "transitionEnabled" : false, "status" : "active", "createdDateTime" : ISODate("2016-10-04T10:06:16.195Z")

这是我的数据结构

{
    "_id" : ObjectId("57f37f18517f72bc09ee7632"),
    "name" : "testdata",
    "createdBy" : "57f1fdef1d3c40141617d215",

    "transitionEnabled" : false,
    "status" : "active",
    "createdDateTime" : ISODate("2016-10-04T10:06:16.195Z"),
    "accounts" : [ 
        "57f37f75517f72bc09ee7634"
    ],
    "deliverables" : [],
    "risks" : [],
    "issues" : {
        "_id" : ObjectId("57f38398517f72bc09ee7680"),
        "title" : "test",
        "description" : "Delay in testing due to issues with Provider Finder dataload in the test region. This has impacted the production release planned for Sep 30th",
        "plannedStartDate" : ISODate("2016-09-01T00:00:00.000Z"),
        "plannedEndDate" : ISODate("2016-10-30T00:00:00.000Z"),
        "assignedTo" : "57f375ea517f72bc09ee762a",
        "createdBy" : ObjectId("57f375ea517f72bc09ee762a"),
        "likes" : 0,
        "createdDateTime" : ISODate("2016-10-04T10:25:28.301Z"),
        "status" : "open",
        "stakeholders" : [],
        "__v" : 0,
        "lastUpdatedTime" : ISODate("2019-11-15T09:19:06.279Z")
    },
    "__v" : 0
}
我希望按组织选择所有问题组,并希望对这些数据实施排序、限制和跳过(子数组问题数据仅来自上面)。为此,我尝试了以下代码

db.organizations.aggregate([
{
“$lookup”:{
“发件人”:“问题”,
“localField”:“issues.str”,
“foreignField”:“\u id.str”,
“作为”:“问题”
}
},
{$sort:{weight:-1,“issues.lastUpdateTime”:1}
{
$group:
{
_id:“$issues”,
},
},
])

我得到的结果如下

如何为下面的查询排序、设置限制和跳过?查询返回的结果也附在后面


但是我不需要上面结果中显示的外部_id字段。请帮助我解决此问题。

您的
排序
不适用于字段
问题。因为
问题
$lookup
之后的对象数组,而不是普通对象。因此,您需要先
$unwind
,然后应用
排序

排序
之后,您应该使用
跳过
限制
。像

{$unwind:"$issues"},
{$sort: {weight: -1, "issues.lastUpdatedTime": 1}},
{ $skip: 10 },// set value as you need 
{ $limit : 50 }// set value as you need 
注意:
$limit
对于
聚合
,始终应在
$skip
之后。所以代码是这样的

db.organizations.aggregate([
  {
    "$lookup": { "from": "issues", "localField": "issues.str", "foreignField": "_id.str", "as": "issues" }
  },
  {$unwind:"$issues"},
  {$sort: {weight: -1, "issues.lastUpdatedTime": 1}},
  {$skip: 10},
  {$limit: 50}
]);

注意:如果您希望在分组后返回固定数量的文档,则应在
$skip
$limit
之后使用
$group
stage

首先$展开问题子数组,并使用其结果应用排序、跳过和限制的可能性应有效。您得到的是什么错误的数据?它没有对数据进行排序wrt lastUpdatedTime,并且在运行此查询后显示了所有记录。跳过和限制也不起作用。始终显示整个数据我想订购上图中显示的_id内的项目,还想对这些数据执行跳过和限制。
skip
limit
应用于一级文档而不是嵌套文档我想获取嵌套数组元素(问题)到外部,并为这些数组元素添加跳过和限制。有什么方法可以做到这一点吗?不需要显示额外的信息,只需显示您拥有的和您想要的,这将比图像更好。解释可以是