配置数据库触发器时,数据库触发器匹配表达式中的$ or选择器在第二层嵌套中不起作用 [英] My $or selector in a database trigger match expression doesn't work at the second level of nesting when configuring a database trigger
问题描述
更新:我使用 $ match表达式来描述这一点,但是我实际上并没有使用$ match运算符。根据文档,选择器应符合$ match的语法,尽管在实际表达式中显然不需要$ match关键字。
Update: I use "$match expression" to describe this but I don't actually use the $match operator. According to the docs, the selector should conform with $match's syntax, though the $match keyword is apparently not necessary in the actual expression.
更新2 :在实际集合 outerField
表示消息
, fieldA
表示 fansNo
,而 fieldB
表示 sharedNo
。因此 outerField.fieldA
表示 message.fansNo
和 outerField.fieldB
表示 message.sharedNo
。这是触发条件触发时 updateDescription
字段的字符串化表示(即,当我仅在 updateDescription.updatedField
中指定匹配表达式):
Update 2: In the actual collection, outerField
represents message
, fieldA
represents fansNo
, and fieldB
represents sharedNo
. So outerField.fieldA
represents message.fansNo
and outerField.fieldB
represents message.sharedNo
. This is a stringified representation of the updateDescription
field when the trigger fires (i.e. when I only specify updateDescription.updatedField
in the match expression):
"updateDescription: {\"removedFields\":[],\"updatedFields\":{\"someOtherField\":310,\"message.fansNo\":1,\"updatedAt\":\"2020-06-22T13:29:08.829Z\"}}"
========================= ======================================
================================================================
原始帖子:
因此,当我指定 message.fansNo
和 message.sharedNo
在匹配表达式中。
So I can't understand why it fails to trigger when I specify message.fansNo
and message.sharedNo
in the match expression.
我正在设置数据库触发器关于集合更新的信息,但我无法获得 $ match
表达式起作用过滤导致触发器触发的更改事件。我想仅在存在两个嵌套字段之一或全部两个时才触发触发器,例如 fieldA
和 fieldB
。这两个字段嵌套在一个对象中,该对象是每个文档中一个字段的值。像这样的东西:
I am setting up a database trigger on updates to a collection, but I'm not able to get my $match
expression to work in filtering the change events that cause the trigger to fire. I want to fire the trigger only if one or both of 2 nested fields are present, say fieldA
and fieldB
. These 2 fields are nested inside an object, and the object is the value of a field in each document. Something like this:
// CollectionA schema
{
_id: ...,
outerField: {
fieldA: 1 // or any number
fieldB: 2 // or any number
},
...
}
我尝试使用以下$ match表达式,但是触发器不会触发:
I have tried using this $match expression below, but the trigger doesn't fire:
{
"$or": [
{
"updateDescription.updatedFields.outerField.fieldA": {"$exists":true}
},
{
"updateDescription.updatedFields.outerField.fieldB":{"$exists":true}
}
]
}
如果我删除了 outerField。< field>
,它会起作用。也就是说:
If I remove outerField.<field>
, it works. That is:
{
"$or": [
{
"updateDescription.updatedFields": {"$exists":true}
},
{
"updateDescription.updatedFields":{"$exists":true}
}
]
}
但是这对我毫无用处,因为触发器会在所有更新时触发。
But of course that's not useful to me because the trigger will fire on any update at all.
我将提供一个演示,但不确定如何创建配置了数据库触发器的示例。
任何帮助将不胜感激,谢谢!
I would provide a demo but I'm not sure how to create a sample that has database triggers configured. Any help will be appreciated, thanks!
推荐答案
所以我能够通过更改查询来解决此问题。监视同时更新但未嵌套的字段。我认为检查嵌套字段的问题是 ChangeEvent
的 updateDescription
属性不包含实际的嵌套对象已更改;而是包含更改的点符号表示。因此,如果您查看我的帖子中的更新2 ,您会看到 updatedFields
具有以下值: {\ ; someOtherField\:310,\ message.fansNo\:1 ...
而不是 {\ someOtherField\ :310,\ message\:{\ fansNo\:1 ...
。通过在$ match查询中使用 message.fansNo
,Mongo将查找以下对象形状: {\ message\ :: { \ fansNo\:1 ...
,在这种情况下不匹配。 真实的解决方案可能是转义匹配表达式中 message.fansNo
中的。
,但是我无法理解工作(请参见此线程)。
So I was able to get around this problem by changing the query to watch for a field that gets updated at the same time but isn't nested. I think the problem with checking for a nested field is that the ChangeEvent
's updateDescription
property doesn't contain the actual nested object that has changed; instead it contains the dot-notation representation of the change. So if you look at Update 2 in my post you'll see that updatedFields
has this value: {\"someOtherField\":310,\"message.fansNo\":1...
instead of {\"someOtherField\":310,\"message\":{\"fansNo\":1...
. By using message.fansNo
in the $match query, Mongo will look for this object shape: {\"message\":{\"fansNo\":1...
, which doesn't match in this case. A "real" solution here could be to escape the .
in message.fansNo
in my match expression, but I couldn't get that to work (see this thread).
因此解决方案对我有用的实际上只是一种针对我的特定用例的解决方法:碰巧总是 someOtherField
与 message.fans一起更新。
和 someOtherField
不嵌套。因此,我可以匹配 someOtherField
而不用担心嵌套。基本上,该匹配表达式为我提供了所需的结果:
So the "solution" that worked for me is really just a workaround that works for my specific use-case: it so happens that someOtherField
is always updated along with message.fansNo
, and someOtherField
isn't nested. So I can match someOtherField
without worrying about nesting. Basically this match expression gives me the results I want:
{
"$or": [
{
"updateDescription.updatedFields.someOtherField": {"$exists":true}
},
{
"updateDescription.updatedFields.someOtherField":{"$exists":true}
}
]
}
希望这对其他人有帮助!
Hope this helps someone else!
这篇关于配置数据库触发器时,数据库触发器匹配表达式中的$ or选择器在第二层嵌套中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!