如何表达如果是else mongodb聚合?

发布于 2025-01-23 15:46:05 字数 551 浏览 0 评论 0原文

假设我们有一个文档结构的集合,

{
   "full": <string>,
   "first": <string>,
   "last": <string>
}

$ project中以下逻辑中寻找表达式

if ("$full" is null or absent)
   concatenate ("$first", "_" , "$last")
else
   "$full"

,我已经设法编写了此内容,但是它在某些情况下无法正常工作,并且看起来很巨大,

$project: {
  "fullName": {
    $cond: {
      if: { "$full": null },
      then: { $concat: [ "$first", "_", "$last" ] }, 
      else: "$full"
    }
  }
}

什么是简洁的在MongoDB聚合中表达这种意图的方法?

Say we have a collection with this document structure

{
   "full": <string>,
   "first": <string>,
   "last": <string>
}

Looking for an expression in $project with the following logic

if ("$full" is null or absent)
   concatenate ("$first", "_" , "$last")
else
   "$full"

I have managed to write this, but it does not work properly for some cases and looks huge

$project: {
  "fullName": {
    $cond: {
      if: { "$full": null },
      then: { $concat: [ "$first", "_", "$last" ] }, 
      else: "$full"
    }
  }
}

What is a concise way to express this intention in MongoDB aggregation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹雪碎 2025-01-30 15:46:05

您可以使用$ ifnull

 {
    $project: {
      full: {
        $ifNull: ["$full", {$concat: ["$first", "_", "$last"]}]
      }
    }
  }

因为您可以看到在这里

You can use $ifNull

 {
    $project: {
      full: {
        $ifNull: ["$full", {$concat: ["$first", "_", "$last"]}]
      }
    }
  }

As you can see here

神也荒唐 2025-01-30 15:46:05

我认为您可以使用$ switch作为的替代方法,如果, else 和然后。这将有助于编写一系列案例。
您可以在此链接中查看此演示工作代码: https://mongopoplay.net/p/num2drknbdy

        $switch: {
          branches: [
            {
              case: {
                $or: [
                  {
                    $lt: [
                      "$full",
                      null
                    ]
                  },
                  {
                    $eq: [
                      "$full",
                      null
                    ]
                  }
                ],
                
              },
              then: {
                "$concat": [
                  "$first",
                  "_",
                  "$last"
                ]
              }
            }
          ],
          default: "$full"
        }

I think you can use $switch as an alternative to if, else and then. It will help to write series of cases.
You can check out this demo working code in this link: https://mongoplayground.net/p/nUM2DRkNbdY

        $switch: {
          branches: [
            {
              case: {
                $or: [
                  {
                    $lt: [
                      "$full",
                      null
                    ]
                  },
                  {
                    $eq: [
                      "$full",
                      null
                    ]
                  }
                ],
                
              },
              then: {
                "$concat": [
                  "$first",
                  "_",
                  "$last"
                ]
              }
            }
          ],
          default: "$full"
        }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文