Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A mongoose plugin implementing the nested set pattern for mongoose models
JavaScript
Tree: 94a1c80610

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
docs
lib
tests
.gitignore
LICENSE
README.md
index.js
package.json

README.md

mongoose-nested-set

A mongoose plugin implementing the nested set pattern for mongoose models

Usage

var mongoose = require('mongoose'),
    NestedSetPlugin = require('mongoose-nested-set'),
    Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/nested_set_test');

var UserSchema = new Schema({
  username: {type: String}
});

// Include plugin
UserSchema.plugin(NestedSetPlugin);

User = mongoose.model('User', UserSchema);

Examples

Examples are based on the following tree:

The Office

User.findOne({username: 'michael'}, function(err, michael) {
  User.rebuildTree(michael, 1, function() {
    // at this point, the tree is built and every node has a lft and rgt value.
    michael.descendants(function(err, data) {
      // data contains a list of michael descendants
      console.log(data);
    });
    console.log('Is Michael a leaf node?', michael.isLeaf());
    console.log('Is Michael a child node?', michael.isChild());
  });
});

API

Static methods

  • Model.rebuildTree(rootNode, leftValueOfRootNode, callback)

Instance methods that return values:

  • isLeaf()

  • isChild()

  • isDescendantOf(otherNode)

  • isAncestorOf(otherNode)

Instance methods that use a callback function:

  • selfAndAncestors(callback)

  • ancestors(callback)

  • selfAndChildren(callback)

  • children(callback)

  • selfAndDescendants(callback)

  • descendants(callback)

  • level(callback)

  • selfAndSiblings(callback)

  • siblings(callback)

Related Links/Resources

Authors

Sponsor

Intellum

Something went wrong with that request. Please try again.