【问题标题】:Or with If and In mongodb或者使用 If 和 In mongodb
【发布时间】:2021-09-16 04:32:39
【问题描述】:
{
  $cond: {
    if: { $in: [`$$${field.fieldName}`, ['', null]] },
    then: [],
    else: `$$${field.fieldName}`,
  },
},

在这种情况下,我也想检查 field.fieldName 退出。

$exists: true,

然后它会像

if ( !filed.fieldName || field.fieldName === null || field.fieldName === '') then []

我没有找到任何添加$exits: truefalse 的方法。 如果可以为此添加一些内容,请提供帮助。或者任何替代方法来实现?

【问题讨论】:

  • 不需要存在条件,因为如果填充不存在则返回null,所以你的null条件就足够了。看到工作playground,你的条件很完美。
  • @turivishal,但在不存在字段的情况下,他想要[] 作为结果,而不是null
  • @WernfriedDomscheit,我认为 else 部分会在该字段不存在时将其删除,因此他想阻止这种情况。

标签: mongodb mongoose mongodb-query


【解决方案1】:

请记住,“field.fieldName 存在”可能与“field.fieldName 不为空”不同。

考虑这样的特殊值:

db.collection.insertMany([{ a: 1 }, { a: '' }, { a: undefined }, { a: null }, {}])
db.collection.aggregate([
   {
      $set: {
         type: { $type: "$a" },
         ifNull: { $ifNull: ["$a", true] },
         defined: { $ne: ["$a", undefined] },
         existing: { $ne: [{ $type: "$a" }, "missing"] }
      }
   }   
])


{ a: 1,         type: double,    ifNull: 1,    defined: true,  existing: true }
{ a: "",        type: string,    ifNull: "",   defined: true,  existing: true }
{ a: undefined, type: undefined, ifNull: true, defined: false, existing: true }
{ a: null,      type: null,      ifNull: true, defined: true,  existing: true }
{               type: missing,   ifNull: true, defined: false, existing: false }

所以,条件可能是这个,这取决于您的要求:

{
   $cond: {
      if: { $ne: [{ $type: "$field.fieldName" }, "missing"] },
      then: [],
      else: "$field.fieldName",
   }
}

【讨论】:

  • 三美元就可以了。我正在编写动态管道代码。只是想添加 $exits。
【解决方案2】:

你只需要把$ifNull放在else部分,如果不存在会返回[]

{
  $cond: {
    if: {
      $in: [`$$${field.fieldName}`, ["", null]]
    },
    then: [],
    else: { $ifNull: [`$$${field.fieldName}`, []] }
  }
}

Playground

输入:

[
  { "key": null },
  { "key": "" },
  { "A": "a" }
]

结果:

[
  {
    "key": null,
    "status": []
  },
  {
    "key": "",
    "status": []
  },
  {
    "status": []
  }
]

第二种方法,如果您想在if 部分添加现有条件,您可以尝试使用$or 的条件,

  • $type 将返回字段的数据类型,缺少的字段类型为“缺失”
{
  $cond: {
    if: {
      $or: [
        { $eq: [{ $type: `$$${field.fieldName}` }, "missing"] },
        { $in: [`$$${field.fieldName}`, ["", null]] }
      ]
    },
    then: [],
    else: `$$${field.fieldName}`
  }
}

Playground

【讨论】:

    【解决方案3】:

    您可以使用$or

    $ifNull

    计算表达式并在表达式计算结果为非空值时返回表达式的值。如果表达式的计算结果为空值,包括未定义值或缺失字段的实例,则返回替换表达式的值。

    { $ifNull: [ <expression>, <replacement-expression-if-null> ] }

    {
        $cond: {
          if: { 
            $or : [
                { $in: [`$$${field.fieldName}`, ['', null]] },
                { $ifNull: [`$$${field.fieldName}`, ""] }
            ]},
          then: [],
          else: `$$${field.fieldName}`,
        }
    },
    

    【讨论】:

    • 我认为不需要 $ifNull 条件,因为如果字段不存在它将返回 null,我认为 OP 的查询是完美的。您可以查看我在上述评论中附加的游乐场。
    • @turivishal 哦,是的,我的错,没注意到。谢谢你。我认为$ifNull 会帮助 OP。
    • 你是对的,我认为这是一个很好的方法。
    【解决方案4】:

    试试这个:

    {
      $cond: {
        if: { $ne : [`$$${field.fieldName}`, undefined] },
        then: [],
        else: `$$${field.fieldName}`,
      },
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-25
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-19
      • 2012-04-29
      • 1970-01-01
      相关资源
      最近更新 更多