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
Javascript 返回MongoDB聚合中包含数据的计数_Javascript_Database_Mongodb_Pipe_Aggregation Framework - Fatal编程技术网

Javascript 返回MongoDB聚合中包含数据的计数

Javascript 返回MongoDB聚合中包含数据的计数,javascript,database,mongodb,pipe,aggregation-framework,Javascript,Database,Mongodb,Pipe,Aggregation Framework,我编写了一个MongoDB聚合查询,它使用了许多阶段。最后,我希望查询以以下格式返回我的数据: { data: // Array of the matching documents here count: // The total count of all the documents, including those that are skipped and limited. } 我将使用skip和limit特性来最终缩减结果。但是,我想知道在跳过并限制它们之前返回的文档数的计

我编写了一个MongoDB聚合查询,它使用了许多阶段。最后,我希望查询以以下格式返回我的数据:

{
    data: // Array of the matching documents here
    count: // The total count of all the documents, including those that are skipped and limited.
}
我将使用skip和limit特性来最终缩减结果。但是,我想知道在跳过并限制它们之前返回的文档数的计数。据推测,管道阶段必须发生在
$match
阶段之后,但在
$skip
$limit
阶段之前

以下是我目前编写的查询(它位于express.js路径中,这就是为什么我要使用这么多变量:

const { 
    minDate, 
    maxDate,
    filter,  // Text to search
    filterTarget, // Row to search for text
    sortBy, // Row to sort by
    sortOrder, // 1 or -1
    skip, // rowsPerPage * pageNumber
    rowsPerPage, // Limit value
} = req.query;



db[source].aggregate([
        {
            $match: { 
                date: {
                    $gt: minDate, // Filter out by time frame...
                    $lt: maxDate
                }
            }
        },
        {
            $match: { 
                [filterTarget]: searchTerm // Match search query....
            }
        },
        {
            $sort: {
                [sortBy]: sortOrder // Sort by date...
            }
        },
        {
            $skip: skip // Skip the first X number of doucuments...
        },
        {
            $limit: rowsPerPage
        },
]);

谢谢你的帮助!

我想我找到了答案。但是如果有人知道这个答案很慢,或者至少在某些方面有问题,请告诉我!

它是添加一个
$group
阶段,将
null
作为值传递,然后将每个文档
$$ROOT
推送到数据数组中,对于每个文档,使用
$sum
操作符将计数增加1

然后,在下一个
$project
阶段,我只需删除
\u id
属性,并对数组进行切片

db[source].aggregate([
        {
            $match: { 
                date: {
                    $gt: minDate, // Filter out by time frame...
                    $lt: maxDate
                }
            }
        },
        {
            $match: { 
                [filterTarget]: searchTerm // Match search query....
            }
        },
        {
            $set: {
                [filterTarget]: { $toLower: `$${filterTarget}` } // Necessary to ensure that sort works properly...
            }
        },
        {
            $sort: {
                [sortBy]: sortOrder // Sort by date...
            }
        },
        {
            $group: { 
                _id: null,
                data: { $push: "$$ROOT" }, // Push each document into the data array.
                count: { $sum: 1 }
            }
        },
        {
            $project: {
                _id: 0,
                count: 1,
                data: { 
                    $slice: ["$data", skip, rowsPerPage]
                },

            }
        }
]).pretty()
我们可以使用在数据上运行并行管道,然后合并每个管道的输出

以下是更新后的查询:

db[source].aggregate([
    {
        $match: { 
            date: {
                $gt: minDate, // Filter out by time frame...
                $lt: maxDate
            }
        }
    },
    {
        $match: { 
            [filterTarget]: searchTerm // Match search query....
        }
    },
    {
        $set: {
            [filterTarget]: { $toLower: `$${filterTarget}` } // Necessary to ensure that sort works properly...
        }
    },
    {
        $sort: {
            [sortBy]: sortOrder // Sort by date...
        }
    },
    {
        $facet:{
            "data":[
                {
                    $skip: skip
                },  
                {
                    $limit:rowsPerPage
                }
            ],
            "info":[
                {
                    $count:"count"
                }
            ]
        }
    },
    {
        $project:{
            "_id":0,
            "data":1,
            "count":{
                $let:{
                    "vars":{
                        "elem":{
                            $arrayElemAt:["$info",0]
                        }
                    },
                    "in":{
                        $trunc:"$$elem.count"
                    }
                }
            }
        }
    }
]).pretty()

将所有文档添加到数组中可能会超过MongoDB单个文档的限制,即16MB。这很有帮助,但我不确定如何将其应用到我的代码中。请解释如何将$match和$sort应用到这个示例中,以便它们与我的请求相关?谢谢。