Is there a way to model recursive Schemas?

3,967 views
Skip to first unread message

trglin88

unread,
Feb 9, 2011, 6:31:39 PM2/9/11
to Mongoose Node.JS ORM
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?

Guillermo Rauch

unread,
Feb 9, 2011, 6:39:17 PM2/9/11
to mongoo...@googlegroups.com
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

Lacek

unread,
Mar 12, 2013, 6:34:09 AM3/12/13
to mongoo...@googlegroups.com
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秒寫道:
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.
Reply all
Reply to author
Forward
0 new messages