'Mongoose find by id ignoring depth level

I have this schema

const personSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  children: {
    type: Array,
    default: [],
    required: false
  }
}, { timestamps: true });

Inside children I can have another person object. ex:

{ name: 'yoshi',
  id: 01,
  children: [
  { name: 'luigi',
    id: 02,
    children: []
  },
  { name: 'mario',
    id: 03,
    children: [
    { name: 'bowser',
      id: 04
      children: []
    }
  ]

If I only have 'bowser' ID (04), how to get 'bowser' object and it's children?

I try Person.findById(04), but I can't get it. I only can get yoshi (01) using findById

Is there a way to ignore all level and focus only getting by ID? I dont want to use find(children.children.id), because I have dynamic depth level.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source