La función de canalización de Mongodb opera matrices, filtrado, interceptación de matrices, ramificación condicional, fusión de cadenas

(La esencia del aprendizaje no es qué conocimiento se recuerda, sino qué desencadena el pensamiento - Michael Sandel)

Insertar descripción de la imagen aquí

Si tenemos la siguiente estructura de datos

La estructura de datos de valor en las características es variable.

{
    
    
  "_id": {
    
    
    "$oid": "64bd0b17258d09faf6ebb6ee"
  },
  "id": {
    
    
    "id": "dania.mndz",
    "platform": "INSTAGRAM"
  },
  "__v": 0,
  "createdAt": {
    
    
    "$date": "2022-08-05T06:42:18.673Z"
  },
  "features": [
    {
    
    
      "type": "SHARED_NICKNAME_AVATAR",
      "value": {
    
    
        "nickname": "Dania Mendez",
        "avatar": "https://cdn.hypeauditor.com/img/instagram/user/15148153672.jpg?w=320&till=1697914801&sign=96da71c49c59693daf2e033bcf70dba3"
      }
    }
  ],
  "updatedAt": {
    
    
    "$date": "2023-07-22T19:00:00.554Z"
  }
}

Operación de función de tubería correspondiente

[
  {
    
    
    $match: {
    
    
      // 筛选日期
      createdAt: {
    
    
        $lte: new Date(1690127999999),
      },
      // 多个同时满足的条件
      $and: [
        {
    
    
          "features.type": "SHARED_EMAIL",
        },
        // value字段必须存在
        {
    
    
          "features.value": {
    
    
            $exists: true,
          },
        },
        // value字段不能为空字符串
        {
    
    
          "features.value": {
    
    
            $ne: "",
          },
        },
        // value字段不能为以下模糊值
        {
    
    
          "features.value": {
    
    
            $nin: [
              /gmail.com/,
              /outlook.com/,
              /yahoo.com/,
              /icloud.com/,
              /mail.qq.com/,
              /qq.com/,
              /mail.ru/,
              /aol.com/,
              /gmx.com/,
              /mail.com/,
              /mail.163.com/,
              /163.com/,
              /protonmail.com/,
              /zoho.com/,
              /yandex.com/,
              /mail.sohu.com/,
              /sohu.com/,
              /hotmail.com/,
              /126.com/,
              /hushmail.com/,
              /tutanota.com/,
              /mail.sina.com.cn/,
              /sina.com.cn/,
              /naver.com/,
              /fastmail.com/,
              /posteo.de/,
            ],
          },
        },
      ],
    },
  },
  {
    
    
    // 排序
    $sort: {
    
    
      createdAt: -1,
    },
  },
  {
    
    
    $project: {
    
    
      name: "$id.id",
      platform: "$id.platform",
      // 对features进行过滤,只获取type等于SHARED_COUNTRY的
      countries: {
    
    
        $filter: {
    
    
          input: "$features",
          as: "doc",
          cond: {
    
    
            $eq: ["$$doc.type", "SHARED_COUNTRY"],
          },
        },
      },
      emails: {
    
    
        $filter: {
    
    
          input: "$features",
          as: "doc",
          cond: {
    
    
            $eq: ["$$doc.type", "SHARED_EMAIL"],
          },
        },
      },
      flowwers: {
    
    
        $filter: {
    
    
          input: "$features",
          as: "doc",
          cond: {
    
    
            $eq: [
              "$$doc.type",
              "SHARED_FOLLOWER_COUNT",
            ],
          },
        },
      },
      categories: {
    
    
        $filter: {
    
    
          input: "$features",
          as: "doc",
          cond: {
    
    
            $eq: [
              "$$doc.type",
              "SHARED_CATEGORY",
            ],
          },
        },
      },
      _id: 0,
    },
  },
  {
    
    
    $project: {
    
    
      name: 1,
      // 对emails进行截取,获取数组内的第一个
      email: {
    
    
        $first: "$emails.value",
      },
      fansCount: {
    
    
        $first: "$flowwers.value",
      },
      platform: 1,
      // 将platform值放入数组中
      platforms: ["$platform"],
      countries: [
        {
    
    
          $first: "$countries.value",
        },
      ],
      categories: {
    
    
        $first: "$categories.value",
      },
      media: {
    
    
        // 使用条件分支
        $switch: {
    
    
          branches: [
            {
    
    	
              // 如果platform的值等于INSTAGRAM
              case: {
    
    
                $eq: ["$platform", "INSTAGRAM"],
              },
              // 则使用$concat操作符将一下三个字符串进行拼接,其中$name是变量值
              then: {
    
    
                $concat: [
                  "https://www.instagram.com/",
                  "$name",
                  "/",
                ],
              },
            },
            {
    
    
              case: {
    
    
                $eq: ["$platform", "YOUTUBE"],
              },
              then: {
    
    
                $concat: [
                  "https://www.youtube.com/channel/",
                  "$name",
                  "/",
                ],
              },
            },
            {
    
    
              case: {
    
    
                $eq: ["$platform", "TIKTOK"],
              },
              then: {
    
    
                $concat: [
                  "https://www.tiktok.com/@",
                  "$name",
                  "/",
                ],
              },
            },
          ],
          // 否则返回默认值
          default: "Did not match",
        },
      },
    },
  },
  {
    
    
    $project: {
    
    
      platform: 0,
    },
  },
]

Ejemplo de devolución

{
    
    
  "name": "___2toua2___",
  "email": "[email protected]",
  "fansCount": 620204,
  "platforms": [
    "INSTAGRAM"
  ],
  "countries": [
    "JP"
  ],
  "categories": [
    "1041",
    "1017"
  ],
  "media": "https://www.instagram.com/___2toua2___/"
}

Supongo que te gusta

Origin blog.csdn.net/qq_42427109/article/details/132210941
Recomendado
Clasificación