how to use $groupby and transform distinct value mongodb

南楼画角 提交于 2020-04-30 06:28:29

问题


How to transform the data using $if $else groupby condition MongoDB?

This playground should return two object who belongs to text with "tester 2" and "tester 3" also if I have multiple object in history collection it should also check with last object not will all object how it is possible

So condition should say if history's date is $gt then main collection should return nothing else return the matched criteria data.

db.main.aggregate([
  {
    $lookup: {
      from: "history",
      localField: "history_id",
      foreignField: "history_id",
      as: "History"
    }
  },
  {
    $unwind: "$History"
  },
  {
    "$match": {
      $expr: {
        $cond: {
          if: {
            $eq: [
              "5e4e74eb380054797d9db623",
              "$History.user_id"
            ]
          },
          then: {
            $and: [
              {
                $gt: [
                  "$date",
                  "$History.date"
                ]
              },
              {
                $eq: [
                  "5e4e74eb380054797d9db623",
                  "$History.user_id"
                ]
              }
            ]
          },
          else: {}
        }
      }
    }
  }
])

MongoPlayground


回答1:


If I understand you correctly, it is what you are trying to do:

db.main.aggregate([
  {
    $lookup: {
      from: "history",
      let: {
        main_history_id: "$history_id",
        main_user_id: { $toString: "$sender_id" }
      },
      pipeline: [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$history_id",
                    "$$main_history_id"
                  ]
                },
                {
                  $eq: [
                    "$user_id",
                    "$$main_user_id"
                  ]
                }
              ]
            }
          }
        }
      ],
      as: "History"
    }
  },
  {
    $unwind: {
      path: "$History",
      preserveNullAndEmptyArrays: true
    }
  },
  {
    $sort: {
      _id: 1,
      "History.history_id": 1,
      "History.date": 1
    }
  },
  {
    $group: {
      _id: "$_id",
      data: { $last: "$$ROOT" },
      History: { $last: "$History" }
    }
  },
  {
    $replaceRoot: {
      newRoot: {
        $mergeObjects: [
          "$data",
          { History: "$History" }
        ]
      }
    }
  },
  {
    "$match": {
      $expr: {
        $or: [
          {
            $eq: [
              { $type: "$History.date" },
              "missing"
            ]
          },
          {
            $ne: [
              "5e4e74eb380054797d9db623",
              "$History.user_id"
            ]
          },
          {
            $and: [
              {
                $eq: [
                  "5e4e74eb380054797d9db623",
                  "$History.user_id"
                ]
              },
              {
                $gte: [
                  "$date",
                  "$History.date"
                ]
              }
            ]
          }
        ]
      }
    }
  }
])

MongoPlayground



来源:https://stackoverflow.com/questions/61497153/how-to-use-groupby-and-transform-distinct-value-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!