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

Himanshu Sharma

Himanshu Sharma 最近创建的主题
Himanshu Sharma 最近回复了
5 年前
回复了 Himanshu Sharma 创建的主题 » MongoDB聚合返回count w/data

我们可以利用 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()
5 年前
回复了 Himanshu Sharma 创建的主题 » MongoDB从文件执行查询并测量时间

可以在文件本身中指定开始时间和结束时间。下面是一个例子:

var start_time = new Date().valueOf();
db.telephone.find({'brand' : 'Apple'});
db.telephone.find({'brand' : 'Samsung'});
var end_time = new Date().valueOf();
print(end_time-start_time);

我们如何精确地测量执行时间?

要分析查询,可以使用 explain() . 它返回查询的完整统计信息。下面是一个例子:

db.telephone.find({'brand' : 'Apple'}).explain("executionStats")

输出:

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "check.telephone",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "brand" : {
                "$eq" : "Apple"
            }
        },
        "winningPlan" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "brand" : {
                    "$eq" : "Apple"
                }
            },
            "direction" : "forward"
        },
        "rejectedPlans" : [ ]
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1,
        "executionTimeMillis" : 35,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 1,
        "executionStages" : {
            "stage" : "COLLSCAN",
            "filter" : {
                "brand" : {
                    "$eq" : "Apple"
                }
            },
            "nReturned" : 1,
            "executionTimeMillisEstimate" : 0,
            "works" : 3,
            "advanced" : 1,
            "needTime" : 1,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "invalidates" : 0,
            "direction" : "forward",
            "docsExamined" : 1
        }
    },
    "serverInfo" : {
        "host" : "theMechanic",
        "port" : 27017,
        "version" : "4.0.11",
        "gitVersion" : "417d1a712e9f040d54beca8e4943edce218e9a8c"
    },
    "ok" : 1
}

注: 这个 executionStats.executionTimeMillis 保留实际查询执行时间。