检查查找管道中的不存在的字段
如何检查字段是否在查找
表达式中是否存在?类似的答案问题 (例如{$ eq:null}
或$已存在:true
)。
例如,我想仅当禁用不存在时,我想查找库存
。
db.orders.aggregate([
{
$lookup: {
from: "inventory",
let: {item: "$item"},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ["$sku", "$$item" ]
},
{
$eq: [ "$disabled", null ]
}
]
}
}
},
],
as: "inv"
}
}
])
一个游乐场样本为在这里
How to check if a field is not existing inside lookup
expression? The answers in similar questions are not helpful (e.g. {$eq : null}
or $exists:true
).
For example, I want to lookup inventory
only if disabled
is not existing.
db.orders.aggregate([
{
$lookup: {
from: "inventory",
let: {item: "$item"},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: ["$sku", "$item" ]
},
{
$eq: [ "$disabled", null ]
}
]
}
}
},
],
as: "inv"
}
}
])
A playground sample is here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在
$ expr
之外使用$已存在
:请参阅游乐场示例
You can use
$exists
outside the$expr
:See how it works on the playground example
您可以使用:
在查找之前,要过滤出您不想查找的文档
you could use:
before the look up, to filter out the documents that you do not want to look up