【问题标题】:Using $if or $ifNull condition inside a $match that will be executed in Jaspersoft Studio在将在 Jaspersoft Studio 中执行的 $match 中使用 $if 或 $ifNull 条件
【发布时间】:2023-03-20 01:45:01
【问题描述】:
{
$match: { 
$expr: {
$or: [
{$ifNull : [$P{departmentuid},"warduid":{"$eq": {"$oid":$P{warduids} } } ] },
{$ifNull : [$P{warduids},"orderdepartmentuid": {"$eq": {"$oid":$P{departmentuid} } } ] }
]
}
}
},

我正在尝试创建一个将从 Jaspersoft Studio 执行的查询。现在,将发送到查询的参数是两个设置参数中的一个,所以我创建了一个 $match,它的条件是它只会过滤将接收的一个参数。 我不太确定我是否对此进行了正确的查询。希望有人可以提供帮助。谢谢!

将从 jaspersoft studio 接收的参数是 $P{departmentuid}${warduids}

逻辑将是,如果查询收到 $P{departmentuid} 参数,它将仅在整个查询中应用 departmentuid,但如果它收到 $P{warduid},它将仅应用 warduid。

【问题讨论】:

标签: mongodb mongodb-query jasper-reports


【解决方案1】:

假设当$P{departmentuid}${warduids}时,会补足null的值。

然后您可以执行以下操作:

db.collection.aggregate([
  {
    $match: {
      $expr: {
        $or: [
          {
            $and: [
              {$eq: [null, $P{warduids}]},
              {$eq: ["$orderdepartmentuid", $P{departmentuid}]}
            ]
          },
          {
            $and: [
              {$eq: [null, $P{departmentuid}]},
              {$eq: ["$warduid", $P{warduids}]}
            ]
          }
        ]
      }
    }
  }
])

$P{warduids} 为空时,这里是Mongo playground

$P{departmentuid} 为空时,这里是Mongo playground

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多