1

I am working on a web application and cannot get myself over my issue. On the website there are categories. Main categories and each category has own subCategories and the subCategories could have also subCategories. All of them are building up by the users. I can build the collection without problem but cannot make my query.

Let's say this is my collection:

[
   {
      categoryName: 'category1',
      otherValue: 'value1',
      subCategories: [
          {
             categoryName: 'category2',
             otherValue: 'value2',
             subCategories: [
                 {
                     categoryName: 'category3',
                     otherValue: 'value3',
                     subCategories: [],
                 }
             ]
          },
          {
             categoryName: 'category4',
             otherValue: 'value4',
             subCategories' : []
          },
          {
             categoryName: 'category5',
             otherValue: 'value5',
             subCategories: [
                 {
                      categoryName: 'category6',
                      otherValue: 'value6',
                      subCategories: []
                 },
                 {
                      categoryName: 'category7',
                      otherValue: 'value7',
                      subCategories: []
                 }
             ]
          }
      ]
   },
   {
      categoryName: 'category8',
      otherValue: 'value8',
      subCategories: [
          {
             categoryName: 'category9',
             otherValue: 'value9',
             subCategories: [
                {
                    categoryName: 'category10',
                    otherValue: 'value10',
                    subCategories: [],
                }
             ]
          },
          {
             categoryName: 'category11',
             otherValue: 'value11',
             subCategories' : []
          },
          {
             categoryName: 'category12',
             otherValue: 'value12',
             subCategories: [
                 {
                      categoryName: 'category13',
                      otherValue: 'value13',
                      subCategories: []
                 }
             ]
          }
      ]
   },
]

I do not want to use .eval() and could not find any other solution to get the result I want. I spent already 3 days on it without success. I hope some one will be able to help me.

The result I would like to get is - only the categoryNames:

[
   {
      categoryName: 'category1',
      subCategories: [
          {
             categoryName: 'category2',
             subCategories: [
                 {
                     categoryName: 'category3'
                 }
             ]
          },
          {
             categoryName: 'category4'
          },
          {
             categoryName: 'category5',
             subCategories: [
                 {
                      categoryName: 'category6'
                 },
                 {
                      categoryName: 'category7'
                 }
             ]
          }
      ]
   },
   {
      categoryName: 'category8',
      subCategories: [
          {
             categoryName: 'category9',
             subCategories: [
                {
                    categoryName: 'category10'
                }
             ]
          },
          {
             categoryName: 'category11'
          },
          {
             categoryName: 'category12',
             subCategories: [
                 {
                      categoryName: 'category13'
                 }
             ]
          }
      ]
   },
]

Any idea appreciated :) Thanks in advance

UPDATE

The way I was trying is:

db.categories.find({ 'subCategories' : {$exists: true, $ne: []}}, { 'categoryName' : 1, '_id' : 0, 'subCategories.categoryName' : 1, 'subCategories.subCategories.categoryName' : 1 });

But on this way I can see only for 2 levels deep and it is a static way. I would like to have a dynamic way where I can map each categoryName inside subCategories and the subCategories of subCategories.

I hope this is clearer.

vitticeps
  • 71
  • 1
  • 6
  • 1
    Where is the code where you called eval and how did you want to use the result? Your question is not very clear. – Jeroen Heier Oct 03 '18 at 17:09
  • I did not use eval(). The way I was going is the .find() function of mongodb. db.categories.find({ 'subCategories' : {$exists: true, $ne: []}}, { 'categoryName' : 1, '_id' : 0, 'subCategories.categoryName' : 1, 'subCategories.subCategories.categoryName' : 1 }); - but it is not dynamic. I can check if the main categories has subCategory and that subCategory has own subCategory and that's it. Cannot go deeper. – vitticeps Oct 03 '18 at 17:14

0 Answers0