社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  MongoDB

MongoDB聚合返回count w/data

Harry Cramer • 5 年前 • 1001 次点击  

我已经编写了一个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
        },
]);

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/56482
 
1001 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Harry Cramer
Reply   •   1 楼
Harry Cramer    5 年前

我想我明白了。但是

是为了增加一个 $group 舞台,传球 null 作为值,然后推送每个文档, $$ROOT ,在数据数组中,对于每个数组,使用 $sum 接线员。

然后,在下一个 $project _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()
Himanshu Sharma
Reply   •   2 楼
Himanshu Sharma    5 年前

我们可以利用 facet 在数据上运行并行管道,然后合并每个管道的输出。

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()