【问题标题】:How to remove a field of an array's nested object that has an empty string value using mongodb aggregation?如何使用 mongodb 聚合删除具有空字符串值的数组嵌套对象的字段?
【发布时间】:2022-08-05 03:45:30
【问题描述】:

enter code here到目前为止,在我尝试之后,我想出了解决方案,如果该对象具有空值字段,我可以删除数组内的整个对象。这在我的情况下不起作用。我只需要删除字段并保留对象的其余部分。在这种情况下,“评论”字段是偶尔有空值的字段。提前致谢!

结构:

someArray [
{
  field1:\"value\",
  field2:\"value\",
  Comment:\"\",
  Answer:\"\",

}, { field1:\"值\", 字段2:\“值\”, 评论:\”\”, 回答:\”\”,

}]

代码:

    $project: {
      someArray: {
        $filter: {
          input: \"$someArray\", as: \"array\",
          cond: { $ne: [ \"$$array.Comment\", \"\"]}}}}
  • 而不是 $filter 尝试使用 $map 运算符。并且,使用$$REMOVE 系统变量来删除一个字段。
  • 您要永久更新文档还是特定于读取操作?
  • 我试过了,但如果 \"Comment\": \"\" 会删除整个对象。具体到读操作。谢谢!
  • 如果我使用 $map 而不是 $filter cond 成为无法识别的参数
  • @prasad_ 我认为$$REMOVE 只删除整个字段而不是数组中的单个元素。

标签: mongodb aggregation-framework


【解决方案1】:

使用$map循环遍历数组元素。对于每个comment不是空字符串的数组元素,返回整个元素,否则返回不包括注释字段的文档。像这样:

db.collection.aggregate([
  {
    "$project": {
      someArray: {
        $map: {
          input: "$someArray",
          as: "element",
          in: {
            $cond: {
              if: {
                $eq: [
                  "",
                  "$$element.Comment"
                ]
              },
              then: {
                field1: "$$element.field1",
                field2: "$$element.field2"
              },
              else: "$$element"
            }
          }
        }
      }
    }
  },
  
])

这是工作的link

【讨论】:

    【解决方案2】:

    这是一个解决方案,其中数组的嵌套对象可以有多个字段,并且这些字段不需要在聚合中引用。删除嵌套对象的字段,其值为空字符串 (""):

    db.collection.aggregate([
    { 
      $set: { 
          someArray: { 
              $map: { 
                  input: '$someArray', 
                  as: 'e',
                  in: {
                      $let: {
                          vars: {
                              temp_var: { 
                                  $filter: { 
                                      input: { $objectToArray: '$$e' }, 
                                      cond: { $ne: [ '', '$$this.v' ] }, 
                                  }
                               }
                          },
                          in: {
                              $arrayToObject: '$$temp_var'
                          }
                      }
                  }
              }
          }
       }
    },
    ])
    

    【讨论】:

    • 也许更好cond: {$not: {$and: [ {$eq: ["$$this.k", "Comment"]}, {$eq: ["$$this.v", ""]} ]}}
    • 也许,如果您的数据有其他字段为空值。最后,是需求问题。
    • @WernfriedDomscheit 仅检查一个字段(在本例中为Comment),将其用于条件:$or: [ { $ne: [ 'Comment', '$$this.k' ] }, { $and: [ { $eq: [ 'Comment', '$$this.k' ] }, { $ne: [ '', '$$this.v' ] } ] } ]
    【解决方案3】:

    Charchit Kapoor 的解决方案仅在您的阵列具有完全

    {
      field1: ...
      field2: ...
      Comment:""
    }
    

    但它不适用于任意字段。我正在寻找更通用的解决方案,我的第一个想法是:

    db.collection.aggregate([
       {
          "$project": {
             someArray: {
                $map: {
                   input: "$someArray",
                   in: {
                      $cond: {
                         if: { $eq: ["$$this.Comment", ""] },
                         then: { $mergeObjects: ["$$this", { Comment: "$$REMOVE" }] },
                         else: "$$this"
                      }
                   }
                }
             }
          }
       }
    ])
    

    但它不起作用。

    我结束了这个:

    db.collection.aggregate([
       {
          "$project": {
             someArray: {
                $map: {
                   input: "$someArray",
                   in: {
                      $cond: {
                         if: { $eq: ["", "$$this.Comment"] },
                         then: {
                            $arrayToObject: {
                               $filter: {
                                  input: {
                                     $map: {
                                        input: { $objectToArray: "$$this" },
                                        as: "element",
                                        in: { $cond: [{ $eq: ["$$element.k", "Comment"] }, null, "$$element"] }
                                     }
                                  },
                                  as: "filter",
                                  cond: "$$filter" // removes null's from array 
                               }
                            }
                         },
                         else: "$$this"
                      }
                   }
                }
             }
          }
       }
    ])
    

    Mongo Playground

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      相关资源
      最近更新 更多