Is there a way to model recursive Schemas?

Showing 1-4 of 4 messages
Is there a way to model recursive Schemas? trglin88 2/9/11 3:31 PM
Heres an abstract example of what I want to do:

var Task = new Schema({
  title     : String,
  body      : String,
  date      : Date,
  subtasks: [Tasks]
});

Is this possible in mongoose?
Re: Is there a way to model recursive Schemas? Guillermo Rauch 2/9/11 3:39 PM
By design, it might work. There's no tests for this. I have `Schema#add` in there for this purpose, to produce recursive references:

var Tasks = new Schema();
Tasks.add({
   title     : String
 , subtasks  : [Tasks]
});

You're welcome to try it out, run into problems, and submit patches/feedback
Thanks!

--
Guillermo Rauch
http://devthought.com
Re: Is there a way to model recursive Schemas? Tim 10/5/12 6:23 PM
I know it's an old post, but can confirm you still need to do .add with recursive schemas having spent last hour wondering why child array was returned as Array[] instead of MongooseDocumentArray.
Re: Is there a way to model recursive Schemas? Lacek 3/12/13 3:34 AM
Thanks Rauch and Tim.
I at first defined the schema using the constructor directly like:
var Tasks = new Schema({
   title     : String
 , subtasks  : [Tasks]
});
Then I got "typeerror cannot call method 'path' of undefined" when updating the object nested in the array.

With Rauch's suggestion it works like a charm.
I know this is a pretty old post but I wish people fallen in the same trap can search the the post easily.

Tim於 2012年10月6日星期六UTC+8上午9時23分27秒寫道: