或者使用 If 和 In mongodb

问题描述 投票:0回答:4
{
  $cond: {
    if: { $in: [`$$${field.fieldName}`, ['', null]] },
    then: [],
    else: `$$${field.fieldName}`,
  },
},

在这种情况下,我还想检查 field.fieldName 是否存在。

 $exists: true,

然后就会像这样

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

我没有找到任何方法来添加

$exits: true
false
。 如果可以添加一些内容,请帮忙。或者有什么替代方法可以实现吗?

mongodb mongoose mongodb-query
4个回答
3
投票

您可以使用$或

$ifNull

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

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

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

3
投票

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

考虑像这样的特殊值:

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 }

2
投票

你只需要把

$ifNull
放在 else 部分,如果不存在就会返回
[]
,

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

游乐场

输入:

[
  { "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}`
  }
}

游乐场


1
投票

试试这个:

{
  $cond: {
    if: { $ne : [`$$${field.fieldName}`, undefined] },
    then: [],
    else: `$$${field.fieldName}`,
  },
}
© www.soinside.com 2019 - 2024. All rights reserved.