如何比较两个集合中的两个值 - pymongo?

发布于 2025-01-20 17:14:30 字数 1716 浏览 2 评论 0原文

我有两个收藏。 例如;

collection1: 
[
  {
    _id: ObjectId("6253492b634e9f3970c5dc86"),
    ID: 90448,
    Name: 'Hostname1',
    Identifier: 'hostname_example1'
  },
  {
    _id: ObjectId("6253492b634e9f3970c5dc87"),
    ID: 66062,
    Name: 'Hostname2',
    Identifier: 'hostname_example2'
  },
  {
    _id: ObjectId("6253492b634e9f3970c5dc88"),
    ID: 56415,
    Name: 'Hostname3',
    Identifier: 'hostname_example3'
  },
  {
    _id: ObjectId("6253492b634e9f3970c5dc89"),
    ID: 84576,
    Name: 'Hostname4',
    Identifier: 'hostname_example4'
  }
]
collection2 =
[
  {
    _id: ObjectId("625350ff09043895b2c3b2d3"),
    flowSourceType: 'Unix',
    flowHostname: 'flowHostname1',
    flowIPd: '11.11.11.11'
  },
    {
    _id: ObjectId("625350ff09043895b2c3b2d3"),
    flowSourceType: 'Unix',
    flowHostname: 'hostname_example2',
    flowIPd: '12.12.12.12'
  },
    {
    _id: ObjectId("625350ff09043895b2c3b2d3"),
    flowSourceType: 'Unix',
    flowHostname: 'hostname_example3',
    flowIPd: '13.13.13.13'
  },
]

我需要这样的比较:

我想搜索-collections1-中标识符的“ flowhostname'-collections2-”的值。 (我的目标是在这里有两件事,1。等于2。包含) 在上面的示例中,我想获取以下数据。

result=
[
    {
    _id: ObjectId("625350ff09043895b2c3b2d3"),
    flowSourceType: 'Unix',
    flowHostname: 'hostname_example2',
    flowIPd: '12.12.12.12'
  },
    {
    _id: ObjectId("625350ff09043895b2c3b2d3"),
    flowSourceType: 'Unix',
    flowHostname: 'hostname_example3',
    flowIPd: '13.13.13.13'
  },
]

如果我以某种方式获得比赛对我来说就足够了。

我该如何使用Python的Pymongo进行操作?

I have two collections.
For example;

collection1: 
[
  {
    _id: ObjectId("6253492b634e9f3970c5dc86"),
    ID: 90448,
    Name: 'Hostname1',
    Identifier: 'hostname_example1'
  },
  {
    _id: ObjectId("6253492b634e9f3970c5dc87"),
    ID: 66062,
    Name: 'Hostname2',
    Identifier: 'hostname_example2'
  },
  {
    _id: ObjectId("6253492b634e9f3970c5dc88"),
    ID: 56415,
    Name: 'Hostname3',
    Identifier: 'hostname_example3'
  },
  {
    _id: ObjectId("6253492b634e9f3970c5dc89"),
    ID: 84576,
    Name: 'Hostname4',
    Identifier: 'hostname_example4'
  }
]
collection2 =
[
  {
    _id: ObjectId("625350ff09043895b2c3b2d3"),
    flowSourceType: 'Unix',
    flowHostname: 'flowHostname1',
    flowIPd: '11.11.11.11'
  },
    {
    _id: ObjectId("625350ff09043895b2c3b2d3"),
    flowSourceType: 'Unix',
    flowHostname: 'hostname_example2',
    flowIPd: '12.12.12.12'
  },
    {
    _id: ObjectId("625350ff09043895b2c3b2d3"),
    flowSourceType: 'Unix',
    flowHostname: 'hostname_example3',
    flowIPd: '13.13.13.13'
  },
]

I need a comparison like this:

I want to search value of 'flowHostname'-collections2- in identifier in -collections1-. (I'm aiming for 2 things here, 1. to be equal, 2. contains)
In the above example, I want to get the following data.

result=
[
    {
    _id: ObjectId("625350ff09043895b2c3b2d3"),
    flowSourceType: 'Unix',
    flowHostname: 'hostname_example2',
    flowIPd: '12.12.12.12'
  },
    {
    _id: ObjectId("625350ff09043895b2c3b2d3"),
    flowSourceType: 'Unix',
    flowHostname: 'hostname_example3',
    flowIPd: '13.13.13.13'
  },
]

It's enough for me if I get the matches somehow.

How can I do this using pymongo with python?

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

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

发布评论

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

评论(1

老娘不死你永远是小三 2025-01-27 17:14:30

只需将相等的$或条件放在第一个$ match阶段的条件中。您可以使用$ limit:1来提高性能。

db.collection2.aggregate([
  {
    "$lookup": {
      "from": "collection1",
      "let": {
        f: "$flowHostname"
      },
      pipeline: [
        {
          "$match": {
            $expr: {
              $or: [
                {
                  $eq: [
                    "$f",
                    "$Identifier"
                  ]
                },
                {
                  $ne: [
                    {
                      "$indexOfCP": [
                        "$Identifier",
                        "$f"
                      ]
                    },
                    -1
                  ]
                }
              ]
            }
          }
        },
        {
          $limit: 1
        }
      ],
      "as": "col1Lookup"
    }
  },
  {
    "$unwind": "$col1Lookup"
  },
  {
    "$unset": "col1Lookup"
  }
])

这是 mongo playground 供您参考。

Simply put the $or condition for equal and contains condition in the first $match stage of subpipeline. You can use $limit: 1 to improve performance.

db.collection2.aggregate([
  {
    "$lookup": {
      "from": "collection1",
      "let": {
        f: "$flowHostname"
      },
      pipeline: [
        {
          "$match": {
            $expr: {
              $or: [
                {
                  $eq: [
                    "$f",
                    "$Identifier"
                  ]
                },
                {
                  $ne: [
                    {
                      "$indexOfCP": [
                        "$Identifier",
                        "$f"
                      ]
                    },
                    -1
                  ]
                }
              ]
            }
          }
        },
        {
          $limit: 1
        }
      ],
      "as": "col1Lookup"
    }
  },
  {
    "$unwind": "$col1Lookup"
  },
  {
    "$unset": "col1Lookup"
  }
])

Here is the Mongo playground for your reference.

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