Home:ALL Converter>How to compare two values in two collections - pymongo?

How to compare two values in two collections - pymongo?

Ask Time:2022-04-11T05:59:51         Author:snorlaxPk

Json Formatter

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?

Author:snorlaxPk,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/71820881/how-to-compare-two-values-in-two-collections-pymongo
yy