如何将具有静态值的字段添加到mongodb查找查询? [英] How to add field with static value to mongodb find query?
问题描述
我们可以向mongodb 查找查询中添加一些具有静态值的自定义字段吗?
Can we add some custom field with static value to mongodb find query?
我正在尝试将API请求UId添加/附加到我们对mongodb进行的所有查询中,以便我们可以使用mongodb日志中的慢速查询来映射请求.
I am trying to add/append API request UId to all the queries that we are making to mongodb, so that we can map requests with slow queries from mongodb logs.
我正在通过在投影字段中使用'$literal'
在聚合查询中执行此操作.
我的汇总查询看起来像:
I am doing it in aggregate queries by using '$literal'
in projected fields.
My Aggregate queries looks something like :
db.test.aggregate({
$project: {
"custom_id": { $literal: "uid" }
..
}
..
})
我也不能将每个字段都单独包含在投影字段中,而不能将"custom_id"字段添加为静态值.
Also I can't include each field individually in projected fields and add the "custom_id" field with the static value.
推荐答案
在mongoDb jira上找到了该问题的答案. 最好的方法是使用$ comment运算符. 这是指向文档的链接.
Found answer to the question on mongoDb jira. The best way to do this would be using the $comment operator. Here's the link to Documentation.
这篇关于如何将具有静态值的字段添加到mongodb查找查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!