MongoDB – Query nested array

I'm having trouble querying document of the following format:

{
    "_id" : ObjectId("6061f989337137b6196dfbf7"),
    "hostname" : "something-rather-random",
    "port_info" : {
            "Interfaces" : {
                    "Interface" : [
                            {
                                    "IfIndex" : "1628",
                                    "Name" : "Tunnel1",
                                    "AbbreviatedName" : "Tun1",
                                    "InOctets" : "0",
                                    "InUcastPkts" : "0",
                                    "InNUcastPkts" : "0",
                                    "InDiscards" : "0",
                                    "InErrors" : "0",
                                    "InUnknownProtos" : "0",
                                    "InRate" : "0",
                                    "OutOctets" : "0",
                                    "OutUcastPkts" : "0",
                                    "OutNUcastPkts" : "0",
                                    "OutDiscards" : "0",
                                    "OutErrors" : "0",
                                    "OutRate" : "0",
                                    "LastClear" : "2021-04-30T14:04:12"
                            },
                            {...}
                           ]
                        }
    },
    "poll_time" : "2021-05-04 14:00:09.065046"

}

Data that i'm trying to get out of this is InErrors for a specific IfIndex (known). Here is how I'm querying:

db.my_db.find({'hostname':'something-rather-random'}, {'port_info.Interfaces.Interface.IfIndex': '1628'}).pretty()

What I'm getting back is a list of dictionaries containing IfIndex'es.



Read more here: https://stackoverflow.com/questions/67386352/mongodb-query-nested-array

Content Attribution

This content was originally published by sngx at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: