【问题标题】:Can anyone know if we can tag the MongoDB queries/commands to know which function of the app ran that query/command?谁能知道我们是否可以标记 MongoDB 查询/命令以了解应用程序的哪个功能运行该查询/命令?
【发布时间】:2022-10-25 20:48:18
【问题描述】:

我在各种函数和 Mongo Atlas 上使用相同的查询(或稍作修改但使用相同的 MongoDB 集合),我们只获得集合名称和查询,但不知道哪个函数实际触发了它。

有没有办法用函数标记查询,以便调试?

【问题讨论】:

  • 为什么不只是在调用查询后记录结果?

标签: javascript node.js mongodb mongoose


【解决方案1】:

我想到了两件事。

第一个是apply a comment 进行查询的能力:

test> db.foo.find({x:123}).comment("unique identifier xyz");
test> db.system.profile.find().sort({ts:-1}).limit(1)
[
  {
    op: 'query',
    ns: 'test.foo',
    command: {
      find: 'foo',
      filter: { x: 123 },
      comment: 'unique identifier xyz',
      lsid: { id: UUID("6ce75fd2-dd4a-4c4e-9655-b80eca8ef755") },
      '$db': 'test'
    },
    keysExamined: 0,
    docsExamined: 10,
    ...

也可以申请an appName in the connection string


上面我演示了the database profiler 是一个可以显示 cmets 的地方(如果已配置)。文档中描述了完整的可用性列表。

对于comments

comment() 将注释字符串与查找操作相关联。这可以更容易地在以下诊断输出中跟踪特定查询:

  • system.profile
  • QUERY 日志组件
  • db.currentOp()

有关mongod 日志、Database Profiler tutorialdb.currentOp() 命令,请参阅configure log verbosity

对于appName

指定自定义应用程序名称。应用名称出现在:

  • mongod 和 mongos 日志
  • currentOp 命令和 db.currentOp() 方法输出中的 currentOp.appName 字段
  • 数据库探查器输出中的 system.profile.appName 字段

appName 连接选项可用于:

  • 从 MongoDB 4.0 开始的 MongoDB 驱动程序
  • 从 mongosh 1.1.9 开始的 mongosh
  • 从 Compass 1.28.4 开始的 MongoDB Compass

【讨论】:

  • 谢谢你。但是您可能还需要为您的数据库启用 ProfilingLevel。 db.setProfilingLevel(2)
  • 谢谢-从文档中添加了有关每个可用性和可配置性的详细信息和链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多