mongoose 或使用If和In mongodb

5us2dqdw  于 2022-11-13  发布在  Go
关注(0)|答案(4)|浏览(162)
{
  $cond: {
    if: { $in: [`$$${field.fieldName}`, ['', null]] },
    then: [],
    else: `$$${field.fieldName}`,
  },
},

在这种情况下,我还想检查字段。fieldName存在。
$exists: true,
那么它将类似于
if ( !filed.fieldName || field.fieldName === null || field.fieldName === '') then []
我没有找到任何方法来添加$exits: truefalse在这。请帮助,如果可以添加一些东西到这。或任何替代的方式来实现?

2nc8po8w

2nc8po8w1#

您可以使用$or
$ifNull
计算表达式,如果表达式的计算结果为非空值,则返回表达式的值。如果表达式的计算结果为空值,包括未定义值或缺少字段的示例,则返回替换表达式的值。
{ $ifNull: [ <expression>, <replacement-expression-if-null> ] }

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

xt0899hw2#

您只需要将$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将返回字段的数据类型,缺少的字段类型为“missing”
{
  $cond: {
    if: {
      $or: [
        { $eq: [{ $type: `$$${field.fieldName}` }, "missing"] },
        { $in: [`$$${field.fieldName}`, ["", null]] }
      ]
    },
    then: [],
    else: `$$${field.fieldName}`
  }
}

Playground

nszi6y05

nszi6y053#

请记住,“field.fieldName exists”可能与“field.fieldName is not null"不同。
考虑如下特殊值:

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

{ _id: 1, a: 1,         type: double,    ifNull: 1,    defined: true,  existing: true }
{ _id: 2, a: "",        type: string,    ifNull: "",   defined: true,  existing: true }
{ _id: 3, a: undefined, type: undefined, ifNull: true, defined: false, existing: true }
{ _id: 4, a: null,      type: null,      ifNull: true, defined: true,  existing: true }
{ _id: 5,               type: missing,   ifNull: true, defined: false, existing: false }

因此,根据您的要求,条件可以是:

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

为了完整起见:使用db.collection.find()

db.collection.find({ a: { $exists: false } })
  { _id: 5 }

db.collection.find({ a: { $exists: true} })
  { _id: 1, a: 1 }, 
  { _id: 2, a: '' }, 
  { _id: 3, a: undefined }, 
  { _id: 4, a: null }

db.collection.find({ a: null })
  { _id: 3, a: undefined }, 
  { _id: 4, a: null },
  { _id: 5 }

db.collection.find({ a: {$ne: null} })
  { _id: 1, a: 1 }, 
  { _id: 2, a: '' }, 

db.collection.find({ a: {$type: "null"} })
  { _id: 4, a: null }
ccrfmcuu

ccrfmcuu4#

试试看:

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

相关问题