Nestedset behaviour for your model
npm install mongoose-nestedset-plugin``bash`
npm install mongoose-nestedset-plugin
`javascript
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
nestedSetPlugin = require('mongoose-nestedset-plugin');
var ItemSchema = new Schema({
title: String
});
ItemSchema.plugin(nestedSetPlugin);
var Item = mongoose.model('Item', ItemSchema);
`
The plugin adds the following fields to your node:
- root - root Id to handle multiple-rooted trees
- lft - left
- rgt - right
- level - nesting level
The method creates a new root node and fails if the ID has been already taken:
`javascript `
Item.addRoot(1, { title: "Root 1" }, function (error, createdRoot){
// handle error or createdRoot node
});
The method add a new node after all the children:
`javascript`
Item.addChild( { _id: rootNodeId }, { title: "Child 1.1" }, function (error, createdChild){
// handle error or createdChild
});
javascript
Item.addAfter( { _id: siblingId }, { title: "Child 1.2" }, function (error, createdItem){
// handle
});
`
$3
`javascript
Item.getChildren( { _id: 123 }, function (error, children){
// handle
});
`$3
`javascript
Item.getDirectChildren( { _id: 123 }, function (error, children){
// handle
});
`$3
`javascript
Item.getTree(function (error, treeNodes){
// handle
});
`$3
`javascript
Item.removeNode( { _id: rootNodeId }, function (error, affected) {
// handle
});
``