Mongodb管道函数操作数组,过滤,数组截取,条件分支,字符串合并

(学习的本质,不在于记住哪些知识,而在于它触发了你的思考——迈克尔·桑德尔)

在这里插入图片描述

假如有以下数据结构

其中features内value的数据结构是可变的

{
    
    
  "_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"
  }
}

对应的管道函数操作

[
  {
    
    
    $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,
    },
  },
]

返回示例

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

猜你喜欢

转载自blog.csdn.net/qq_42427109/article/details/132210941