如何打印或记录实际执行的查询语句,如django插入,查询和其他SQL语句将被记录

问题描述 投票:0回答:1

将如何记录或打印实际执行的查询语句,例如django insert,query和其他SQL语句。我搜索了两个库的文档,却一无所获。

我想要类似于django的东西,因为我可以执行任何查询,例如User.object.filter(name='123').values("id", "phone"),并记录该查询的实际执行语句:select id, phone from auth_user where name='123'

python mongodb pymongo mongoengine
1个回答
0
投票

我相信您有2个选择:

1)使用pymongo.monitoring,MongoEngine的文档中的此link显示了如何配置它。

2)用db.setProfilingLevel(2)打开分析,然后所有查询将添加到db.system.profile collection。例如:执行此db.users.findOne({'name': 'abc'}),会将以下内容添加到db.system.profile集合中:

{
    "op" : "query",
    "ns" : "myDb.users",
    "command" : {
        "find" : "users",
        "filter" : {
            "name" : "abc"
        },
        "limit" : 1,
        ....
        "$db" : "myDb"
    },
    "keysExamined" : 0,
    "docsExamined" : 0,
    "cursorExhausted" : true,
    "numYield" : 0,
    "locks" : {...},
    "nreturned" : 0,
    "responseLength" : 83,
    "protocol" : "op_msg",
    "millis" : 0,
    "planSummary" : "EOF",
    ...
    "ts" : ISODate("2019-11-20T19:27:13.297Z"),
    "appName" : "MongoDB Shell",
    "allUsers" : [ ],
    "user" : ""
}

您可以看到,不幸的是,它不像SQL查询那样实用,但是信息已经存在

© www.soinside.com 2019 - 2024. All rights reserved.