Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Mongoose Tree Schema
// An excerpt from a Mongoose model.js file for use with Express:
// Sub-document to store parent ref along with it's value (a form of caching)
var Parent = new Schema({
id: ObjectId
, text: String
// Main tree-node element schema
var Branch = new Schema({
text: {
type: String
, required: true }
, date: {type: Date, default: }
, trail: [Parent]
, parentBranchId: ObjectId
, parentBranch: { type: Schema.Types.ObjectId, ref: 'Branch' }
, _children: [{type: Schema.Types.ObjectId, ref: 'Branch'}]
// These two have been commented out because I have no clue how to best implement
// , _priorSiblings: { type: Schema.Types.ObjectId, ref: 'Branch' }
// , _followingSiblings: { type: Schema.Types.ObjectId, ref: 'Branch' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment