如何比较MongoDB中不同集合的字段

发布于 2025-02-09 04:04:31 字数 934 浏览 1 评论 0 原文

在这里,我有来自多个表的多个字段,这些值需要比较,需要显示所需的结果。

SQL查询:

select pd.service_id,ps.service_id from player pd, service ps where pd.subject_id=ps.subject_id and pd.service_id = ps.service_id

Mongo查询:

db.player.aggregate([
        {
          "$lookup":{
            "from":"service",
            "localField":"player.subject_id",
            "foreignField":"subject_id",
            "as":"ps"
          }
        },
        {
          "$unwind":"$ps"
        },
        {
          "$match":{
           "service_id":{
            "$eq": "ps.service_id"
           }
          }
        }  
      ];

示例输入记录:

播放器:

[{subject_id:23,service_id:1},{subject_id:76,service_id:9}]

服务:

[{subject_id:76,service_id:9},{subject_id:99,service_id:10}]

比赛不起作用。我必须匹配两个集合的service_id。需要获得匹配的记录。但看不到任何结果。任何人都可以帮我找出错误...

Here, I have multiple fields from multiple tables those values needs to compared and need to display desired result.

SQL QUERY:

select pd.service_id,ps.service_id from player pd, service ps where pd.subject_id=ps.subject_id and pd.service_id = ps.service_id

Mongo query:

db.player.aggregate([
        {
          "$lookup":{
            "from":"service",
            "localField":"player.subject_id",
            "foreignField":"subject_id",
            "as":"ps"
          }
        },
        {
          "$unwind":"$ps"
        },
        {
          "$match":{
           "service_id":{
            "$eq": "ps.service_id"
           }
          }
        }  
      ];

sample input records:

player:

[{subject_id:23,service_id:1},{subject_id:76,service_id:9}]

service:

[{subject_id:76,service_id:9},{subject_id:99,service_id:10}]

The match is not working. I have to match service_id's of both collections. Need to get matched records. But not able to see any result. Can anyone please help me to find out the mistake...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骑趴 2025-02-16 04:04:31

在查询中,如果要比较文档本身的2个值,则需要使用 $ expr 操作员

{
    "$match":{
       "$expr":{
            "$eq": ["$service_id", "$ps.service_id"]
       }
    }
} 

mongoplay

' >您需要使用 to“*加入”带2 o更多条件

db.player.aggregate([
  {
    "$lookup": {
      "from": "service",
      "let": {
        subject_id: "$subject_id",
        service_id: "$service_id"
      },
      "pipeline": [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$subject_id",
                    "$subject_id"
                  ]
                },
                {
                  $eq: [
                    "$service_id",
                    "$service_id"
                  ]
                }
              ]
            }
          }
        }
      ],
      "as": "ps"
    }
  },
  // Remove non matched results
  {
    $match: {
      "ps.0": {
        $exists: true
      }
    }
  },
  // Remove temporal "ps" field
  {
    $addFields: {
      "ps": "$REMOVE"
    }
  }
])

In your query, if you want to compare 2 values from the document itself, you need to use $expr operator

{
    "$match":{
       "$expr":{
            "$eq": ["$service_id", "$ps.service_id"]
       }
    }
} 

MongoPlayground

Alternative solution: You need to use Uncorrelated sub-query to "* join" with 2 o more conditions

db.player.aggregate([
  {
    "$lookup": {
      "from": "service",
      "let": {
        subject_id: "$subject_id",
        service_id: "$service_id"
      },
      "pipeline": [
        {
          $match: {
            $expr: {
              $and: [
                {
                  $eq: [
                    "$subject_id",
                    "$subject_id"
                  ]
                },
                {
                  $eq: [
                    "$service_id",
                    "$service_id"
                  ]
                }
              ]
            }
          }
        }
      ],
      "as": "ps"
    }
  },
  // Remove non matched results
  {
    $match: {
      "ps.0": {
        $exists: true
      }
    }
  },
  // Remove temporal "ps" field
  {
    $addFields: {
      "ps": "$REMOVE"
    }
  }
])

MongoPlayground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文