如何使用$ groupby并转换不同的值mongodb [英] how to use $groupby and transform distinct value mongodb

查看:87
本文介绍了如何使用$ groupby并转换不同的值mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 $ if $ else groupby条件MongoDB转换数据?

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

这个游乐场应该返回两个属于带有 tester 2和 tester 3的文本的对象,如果我在历史记录集合中有多个对象,则还应该与最后一个对象进行检查,否则所有对象都将不可能。

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

因此条件应该说,如果历史记录的日期是 $ gt ,则主集合应该不返回任何内容,返回匹配的条件数据。

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

推荐答案

如果我正确理解您的意思,那就是您要尝试做的事情:

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

这篇关于如何使用$ groupby并转换不同的值mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆