社区所有版块导航
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
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

Harry Cramer

Harry Cramer 最近创建的主题
Harry Cramer 最近回复了
4 年前
回复了 Harry Cramer 创建的主题 » MongoDB聚合返回count w/data

我想我明白了。但是

是为了增加一个 $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()