[Python] How to compare two values in two collections - pymongo?

Discussão em 'Python' iniciado por Stack, Julho 25, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página