Skip to content

Instantly share code, notes, and snippets.

@eethann
Created October 14, 2012 19:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eethann/3889616 to your computer and use it in GitHub Desktop.
Save eethann/3889616 to your computer and use it in GitHub Desktop.
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: Date.now }
, 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