0

Is it possible with mongoose to create a schema, call it Folders and it has a property within called subfolders that is an array of nested Folder subdocs?

const mongoose = require('mongoose')

let SubSchema = mongoose.Schema({
    name: { type: String, required: true }
})

let FolderSchema = mongoose.Schema({
    name: { type: String, required: true },
    children: [ SubSchema ]
})

I know I can nest subdocs in an array by referencing another schema similar to what is shown above. What I'm looking to do though is reuse FolderSchema within itself. Obviously this causes an issue because at the time of creation of the schema, FolderSchema doesn't exist. Is there a better way to do this? Is there a way to recursively nest documents using the same schema?

I know I could have the array be a list of ObjectId that reference a collection but I was hoping to just keep it all nested as documents. I guess if I did populate() to let it resolve the doc ids, that would essentially be the same thing. Just wondering if there is another method I wasn't aware of.

dferg
  • 97
  • 1
  • 2
  • 10

3 Answers3

6

I haven't tried this personally, but I have read this can be achieved by simply referencing this in the field you want to reference the current model.

The model you would like to will look something like this,

const mongoose = require('mongoose')

const FolderSchema = mongoose.Schema({
    name: { type: String, required: true },
    type: { type: String, enum: ['file', 'directory'],
    children: [ this ]
})

const FolderModel = mongoose.model('Folder', FolderSchema);

Hope that helps!

bharadhwaj
  • 2,059
  • 22
  • 35
0

look you need to clarify your question a little bit but as much as i understood from the question, yes it can be done in this way :

var mongoose = require('mongoose');
var FolderSchema = new mongoose.Schema({
 SubFolders = [ type:monogoose.Schema.Types.ObjectId, ref : 'Folders']
 });
var folder = mongoose.model('Folders',FolderSchema);
  module.exports = folder;

This shall work for you.

Raghav
  • 31
  • 4
0

So for infinite object having children, I did it like so:

mongoose schema:

const itemSchema = new mongoose.Schema({
  name: String,
  items: {
    type: [this],
    default: undefined
  }
}, { _id: false })

const mainSchema = new mongoose.Schema({
  name: String,
  items: {
    type: [itemSchema],
    default: undefined
  }
})

output example:

[
  {
    _id: '62a72d6915ad7f79d738e465',
    name: 'item1',
    items: [
      {
        name: 'item1-item1',
        items: [
          {
            name: 'item1-item1-item1'
          },
          {
            name: 'item1-item1-item2'
          },
          {
            name: 'item1-item1-item3'
          },
        ]
      },
      {
        name: 'item1-item2'
      }
    ]
  },
  {
    _id: '62a72d6915ad7f79d738e467',
    name: 'item2'
  },
  {
    _id: '62a72d6915ad7f79d738e467',
    name: 'item3',
    items: [
      {
        name: 'item3-item1'
      }
    ]
  }
]
Miko Chu
  • 1,087
  • 14
  • 22