mongoose-data-tree
This project only upgrade dependecies package modules - fix issue Error: Cannot find module '../build/Release/bson'
mongoose-path-tree infomation
Implements the materialized path strategy with cascade child re-parenting on delete for storing a hierarchy of documents with mongoose Version with all collected features and fixes from mongoose-tree, mongoose-tree-fix, mongoose-tree2, mongoose-reparenting-tree
Usage
Install via NPM
$ npm install mongoose-data-tree
Options
Model
Then you can use the plugin on your schemas
var tree = ; var UserSchema = name : String;UserSchema;var User = mongoose; var adam = name : 'Adam' ;var bob = name : 'Bob' ;var carol = name : 'Carol' ; // Set the parent relationshipsbobparent = adam;carolparent = bob; adam;
At this point in mongoDB you will have documents similar to
{
"_id" : ObjectId("50136e40c78c4b9403000001"),
"name" : "Adam",
"path" : "50136e40c78c4b9403000001"
}
{
"_id" : ObjectId("50136e40c78c4b9403000002"),
"name" : "Bob",
"parent" : ObjectId("50136e40c78c4b9403000001"),
"path" : "50136e40c78c4b9403000001#50136e40c78c4b9403000002"
}
{
"_id" : ObjectId("50136e40c78c4b9403000003"),
"name" : "Carol",
"parent" : ObjectId("50136e40c78c4b9403000002"),
"path" : "50136e40c78c4b9403000001#50136e40c78c4b9403000002#50136e40c78c4b9403000003"
}
The path is used for recursive methods and is kept up to date by the plugin if the parent is changed
API
getChildren
Signature:
getChildren([filters], [fields], [options], [recursive], cb);
args are additional filters if needed. if recursive is supplied and true, subchildren are returned
Based on the above hierarchy:
adam; adam;
getChildrenTree
Signature as method:
getChildrenTree([args], cb);
Signature as static:
getChildrenTree([rootDoc], [args], cb);
return a recursive tree of sub-children.
args is an object you can defined with theses properties :
filters: mongoose query filter, optional, default null
example: filters: {owner:myId}
fields: mongoose fields, optional, default null (all fields)
example: fields: "_id name owner"
options: mongoose query option, optional, default null
example: options:{{sort:'-name'}}
minLevel: level at which will start the search, default 1
example: minLevel:2
recursive: boolean, default true
make the search recursive or only fetch children for the specified level
example: recursive:false
allowEmptyChildren: boolean, default true
if true, every child not having children will have 'children' attribute (empty array)
if false, every child not having children will not have 'children' attribute
Example :
var args = filters: owner:myId fields: "_id name owner" minLevel:2 recursive:true allowEmptyChildren:false ;
Based on the above hierarchy:
adam;
getAncestors
Signature:
getAncestors([filters], [fields], [options], cb);
Based on the above hierarchy:
carol
level
Equal to the level of the hierarchy
carollevel; // equals 3
Tests
To run the tests install mocha
npm install mocha -g
and then run
mocha