有什么方法可以将元数据添加到 MongoDB 聚合查询吗?
Is there any way to add metadata to MongoDB Aggregation Queries?
有很多应用程序访问 MongoDB 集群,缓慢的查询很难跟踪。特别是当它是聚合管道时。
我们正在使用 MongoDB Atlas,其中我们有一个分析器,它显示 运行 超出阈值(比如 5000 毫秒)的已执行查询,并且还显示相应的集合。我们可以查看缓慢的查询,但它没有给出任何提示是哪个应用程序调用了它。它需要在整个解决方案的整个应用程序中对相关查询进行“查找”。很费时间!
目前我正在使用 MongoDB C# 驱动程序。
有什么方法可以 insert/add MongoDB 查询中的任何“元数据”,如“名称”或“调用应用程序”等,并从慢速查询部分取回,还是来自查询日志?
以下是示例数据库日志(已编辑凭据,仅供参考)
[conn280813] command <database>.<collection> command: aggregate
{ aggregate: "<collection>", pipeline: [ { $match: { <collection>.
<property2>: "XXXXXXXXXXXX", <collection>.<property3>:
"XXXXXXXXXXXXXXXXX", <collection>.<property4>: "XXXXXXX",
<collection>.<property5>: "XX", <collection>.<property6>: { $gte: 1
}, <collection>.<property7>: { $gte: new Date(1636730882184) } } },
{ $group: { _id: "$<collection>.<property>", count: { $sum:
"$<collection>.<property>" }, <aggregate-field>: { $first:
"$<collection>.<property>" } } }, { $sort: { count: -1 } }, {
$lookup: { from: "<collection>", let: { <out-field>: "$<property>"
}, pipeline: [ { $match: { $expr: { $eq: [ "$_id", "$$<property>" ]
} } }, { $graphLookup: { from: "<collection>", startWith: "$_id",
connectFromField: "<property>", connectToField: "_id", as: "
<object>", depthField: "level" } }, { $unwind: "$<object>" }, {
$unwind: "$<object>.<property>" }, { $match: { <object>.<property>:
<value> } }, { $group: { _id: "$<object>.<property>", <property>: {
$push: "$<object>.<property>" }, protectionLevel: { $push:
"$<object>.<property>" } } }, { $project: { _id: 0, <property>:
"$_id", <property2>: { $in: [ true, "$<property>" ] }, <property>:
{ $in: [ 1, "$<property>" ] } } } ], as: "<out-property>" } }, {
$unwind: { path: "$<property>" } }, { $match: { <object>.
<property>: false } }, { $limit: 50 }, { $lookup: { from: "
<collection>", localField: "_id", foreignField: "_id", as: "<out-
object>" } }, { $unwind: { path: "$<property>" } }, { $project: {
<property>: "$_id", count: 1, <property2>: 1, <property3>:
"$<object>.<property>" } }, { $unwind: { path: "$<property>" } }, {
$match: { <object>.<property>: "<filter-value>", <object>.
<property>: <value>, <object>.<property>: { $gt: 0 } } }, {
$project: { <collection>.<property>: { $arrayElemAt: [ { $map: {
input: { $filter: { input: "$<object>.<property>", as: "version",
cond: { $eq: [ "$<object>.<property>", "$$<object>.<property>" ] }
} }, as: "v", in: "$$<property>" } }, 0 ] }, <out-field>:
"$<property>", <out-field>: "$_id", <out-field>: "$<property>",
_id: 0 } }, { $lookup: { from: "<collection>", localField: "
<collection>.<property>", foreignField: "_id", as: "<collection>" }
}, { $project: { <out-field>: { $arrayElemAt: [ "$<collection>.
<property>", 0 ] }, <out-field>: 1, <out-field>: 1, <out-field>: 1
} }, { $limit: 5 } ], allowDiskUse: false, cursor: {}, $db: "<db>",
lsid: { id: UUID("XXXXXXXX-XXXXX-XXXX") }, $clusterTime: {
clusterTime: Timestamp(1644506882, 1), signature: { hash:
BinData(0, XXXXXXXXXXXXXXXXXXXXXXXXXX), keyId: XXXXXXXXXXXXX } } }
planSummary: IXSCAN { <object>.<property>: 1, <object>.<property>:
1, <object>.<property>: 1 } keysExamined:12476 docsExamined:12475
hasSortStage:1 cursorExhausted:1 numYields:99 nreturned:5
reslen:1138 locks:{ Global: { acquireCount: { r: 193 } }, Database:
{ acquireCount: { r: 193 } }, Collection: { acquireCount: { r: 192
} } } storage:{ data: { bytesRead: 10641559, timeReadingMicros:
59687 } } protocol:op_msg 263ms
提前致谢。
您可以在日志中看到连接 ID:conn280813,如果您通过此 ID 搜索,您将找到建立连接的 IP 和用户,最佳做法是为每个应用程序创建用户,以便用户确定哪个应用正在连接...
ACCESS 消息如下所示:
2022-02-14T09:27:26.423+0100 I ACCESS [conn2800813] Successfully authenticated as principal <the_user_name> on admin from client <CLIENT_IP_ADDRESS>:<CLIENT_PORT_NO>
使用$comment for this target. You can specify this option in various ways, for aggregation it can be set via AggregateOptions
有很多应用程序访问 MongoDB 集群,缓慢的查询很难跟踪。特别是当它是聚合管道时。
我们正在使用 MongoDB Atlas,其中我们有一个分析器,它显示 运行 超出阈值(比如 5000 毫秒)的已执行查询,并且还显示相应的集合。我们可以查看缓慢的查询,但它没有给出任何提示是哪个应用程序调用了它。它需要在整个解决方案的整个应用程序中对相关查询进行“查找”。很费时间!
目前我正在使用 MongoDB C# 驱动程序。
有什么方法可以 insert/add MongoDB 查询中的任何“元数据”,如“名称”或“调用应用程序”等,并从慢速查询部分取回,还是来自查询日志?
以下是示例数据库日志(已编辑凭据,仅供参考)
[conn280813] command <database>.<collection> command: aggregate
{ aggregate: "<collection>", pipeline: [ { $match: { <collection>.
<property2>: "XXXXXXXXXXXX", <collection>.<property3>:
"XXXXXXXXXXXXXXXXX", <collection>.<property4>: "XXXXXXX",
<collection>.<property5>: "XX", <collection>.<property6>: { $gte: 1
}, <collection>.<property7>: { $gte: new Date(1636730882184) } } },
{ $group: { _id: "$<collection>.<property>", count: { $sum:
"$<collection>.<property>" }, <aggregate-field>: { $first:
"$<collection>.<property>" } } }, { $sort: { count: -1 } }, {
$lookup: { from: "<collection>", let: { <out-field>: "$<property>"
}, pipeline: [ { $match: { $expr: { $eq: [ "$_id", "$$<property>" ]
} } }, { $graphLookup: { from: "<collection>", startWith: "$_id",
connectFromField: "<property>", connectToField: "_id", as: "
<object>", depthField: "level" } }, { $unwind: "$<object>" }, {
$unwind: "$<object>.<property>" }, { $match: { <object>.<property>:
<value> } }, { $group: { _id: "$<object>.<property>", <property>: {
$push: "$<object>.<property>" }, protectionLevel: { $push:
"$<object>.<property>" } } }, { $project: { _id: 0, <property>:
"$_id", <property2>: { $in: [ true, "$<property>" ] }, <property>:
{ $in: [ 1, "$<property>" ] } } } ], as: "<out-property>" } }, {
$unwind: { path: "$<property>" } }, { $match: { <object>.
<property>: false } }, { $limit: 50 }, { $lookup: { from: "
<collection>", localField: "_id", foreignField: "_id", as: "<out-
object>" } }, { $unwind: { path: "$<property>" } }, { $project: {
<property>: "$_id", count: 1, <property2>: 1, <property3>:
"$<object>.<property>" } }, { $unwind: { path: "$<property>" } }, {
$match: { <object>.<property>: "<filter-value>", <object>.
<property>: <value>, <object>.<property>: { $gt: 0 } } }, {
$project: { <collection>.<property>: { $arrayElemAt: [ { $map: {
input: { $filter: { input: "$<object>.<property>", as: "version",
cond: { $eq: [ "$<object>.<property>", "$$<object>.<property>" ] }
} }, as: "v", in: "$$<property>" } }, 0 ] }, <out-field>:
"$<property>", <out-field>: "$_id", <out-field>: "$<property>",
_id: 0 } }, { $lookup: { from: "<collection>", localField: "
<collection>.<property>", foreignField: "_id", as: "<collection>" }
}, { $project: { <out-field>: { $arrayElemAt: [ "$<collection>.
<property>", 0 ] }, <out-field>: 1, <out-field>: 1, <out-field>: 1
} }, { $limit: 5 } ], allowDiskUse: false, cursor: {}, $db: "<db>",
lsid: { id: UUID("XXXXXXXX-XXXXX-XXXX") }, $clusterTime: {
clusterTime: Timestamp(1644506882, 1), signature: { hash:
BinData(0, XXXXXXXXXXXXXXXXXXXXXXXXXX), keyId: XXXXXXXXXXXXX } } }
planSummary: IXSCAN { <object>.<property>: 1, <object>.<property>:
1, <object>.<property>: 1 } keysExamined:12476 docsExamined:12475
hasSortStage:1 cursorExhausted:1 numYields:99 nreturned:5
reslen:1138 locks:{ Global: { acquireCount: { r: 193 } }, Database:
{ acquireCount: { r: 193 } }, Collection: { acquireCount: { r: 192
} } } storage:{ data: { bytesRead: 10641559, timeReadingMicros:
59687 } } protocol:op_msg 263ms
提前致谢。
您可以在日志中看到连接 ID:conn280813,如果您通过此 ID 搜索,您将找到建立连接的 IP 和用户,最佳做法是为每个应用程序创建用户,以便用户确定哪个应用正在连接...
ACCESS 消息如下所示:
2022-02-14T09:27:26.423+0100 I ACCESS [conn2800813] Successfully authenticated as principal <the_user_name> on admin from client <CLIENT_IP_ADDRESS>:<CLIENT_PORT_NO>
使用$comment for this target. You can specify this option in various ways, for aggregation it can be set via AggregateOptions