Chartjs-Removing Dynamic Fields by Association in MongoDB Aggregation

0๐Ÿ‘

โœ…

  • $project merge two array with the same position
  • $set filter array
  • $addFields recover the original array
  • $project remove the merge array

aggregate

db.collection.aggregate([
  {
    $project: {
      anotherValue: {
        $map: {
          input: {
            $range: [
              0,
              {
                $size: "$A_set"
              }
            ]
          },
          as: "idx",
          in: {
            $mergeObjects: [
              {
                $arrayElemAt: [
                  "$A_set",
                  "$$idx"
                ]
              },
              {
                $arrayElemAt: [
                  "$A_count_set",
                  "$$idx"
                ]
              }
            ]
          }
        }
      }
    }
  },
  {
    $set: {
      anotherValue: {
        $filter: {
          input: "$anotherValue",
          as: "x",
          cond: {
            "$ne": [
              "$$x.A",
              "N"
            ]
          }
        }
      }
    }
  },
  {
    $addFields: {
      "A_set": {
        $map: {
          input: "$anotherValue",
          as: "a",
          in: {
            "A": "$$a.A"
          }
        }
      },
      "A_count_set": {
        $map: {
          input: "$anotherValue",
          as: "a",
          in: {
            "A_count": "$$a.A_count"
          }
        }
      }
    }
  },
  {
    "$project": {
      "anotherValue": 0
    }
  }
])

mongoplayground

Leave a comment