How to query for a field name that can be in different levels in mongodb

339 views Asked by At

Is it possible to query MongoDB for values of a specific field-name disregarding where it's located in the document structure?

For example If I have this document: https://mongoplayground.net/p/ANSezUHhueD

{
  device_name: "host1",
  ip: 192.168.1.10,
  connected_hosts: [
    {
      name: "host2",
      ip: 192.168.1.11
    },
    {
      name: "host2",
      ip: 192.168.1.12
    }        ​
 ​]
}

Can I look for all the fields named "ip" no matter where they are defined, so I get a result that lists all 3 IPs [192.168.1.10, 192.168.1.11, 192.168.1.12]?

In my use case the nested document {name, ip} can appear in additional different other nested documents.

0

There are 0 answers