Skip to content

Commit

Permalink
Added examples with Nested Sets and Materialized path.
Browse files Browse the repository at this point in the history
  • Loading branch information
Voronenko committed Jan 6, 2013
1 parent fe5e0c9 commit abc9e0e
Show file tree
Hide file tree
Showing 15 changed files with 459 additions and 0 deletions.
27 changes: 27 additions & 0 deletions MaterializedPathReference.js
@@ -0,0 +1,27 @@
use TreeMongo;
db.categoriesMP.drop();
db.categoriesMP.insert({_id:"Electronics",path:""});
db.categoriesMP.insert({_id:"Cameras_and_Photography",path:"Electronics,"});
db.categoriesMP.insert({_id:"Digital_Cameras",path:"Electronics,Cameras_and_Photography,"});
db.categoriesMP.insert({_id:"Camcorders",path:"Electronics,Cameras_and_Photography,"});
db.categoriesMP.insert({_id:"Lenses_and_Filters",path:"Electronics,Cameras_and_Photography,"});
db.categoriesMP.insert({_id:"Tripods_and_supports",path:"Electronics,Cameras_and_Photography,"});
db.categoriesMP.insert({_id:"Lighting_and_studio",path:"Electronics,Cameras_and_Photography,"});

db.categoriesMP.insert({_id:"Shop_Top_Products",path:"Electronics,"});
db.categoriesMP.insert({_id:"IPad",path:"Electronics,Shop_Top_Products,"});
db.categoriesMP.insert({_id:"IPhone",path:"Electronics,Shop_Top_Products,"});
db.categoriesMP.insert({_id:"IPod",path:"Electronics,Shop_Top_Products,"});
db.categoriesMP.insert({_id:"Blackberry",path:"Electronics,Shop_Top_Products,"});

db.categoriesMP.insert({_id:"Cell_Phones_and_Accessories",path:"Electronics,"});
db.categoriesMP.insert({_id:"Cell_Phones_and_Smartphones",path:"Electronics,Cell_Phones_and_Accessories,"});
db.categoriesMP.insert({_id:"Headsets",path:"Electronics,Cell_Phones_and_Accessories,"});
db.categoriesMP.insert({_id:"Batteries",path:"Electronics,Cell_Phones_and_Accessories,"});
db.categoriesMP.insert({_id:"Cables_And_Adapters",path:"Electronics,Cell_Phones_and_Accessories,"});

db.categoriesMP.insert({_id:"Nokia",path:"Electronics,Cell_Phones_and_Accessories,Cell_Phones_and_Smartphones,"});
db.categoriesMP.insert({_id:"Samsung",path:"Electronics,Cell_Phones_and_Accessories,Cell_Phones_and_Smartphones,"});
db.categoriesMP.insert({_id:"Apple",path:"Electronics,Cell_Phones_and_Accessories,Cell_Phones_and_Smartphones,"});
db.categoriesMP.insert({_id:"HTC",path:"Electronics,Cell_Phones_and_Accessories,Cell_Phones_and_Smartphones,"});
db.categoriesMP.insert({_id:"Vyacheslav",path:"Electronics,Cell_Phones_and_Accessories,Cell_Phones_and_Smartphones,"});
14 changes: 14 additions & 0 deletions MaterializedPathReference_nodedescendants.js
@@ -0,0 +1,14 @@
use TreeMongo;

var descendants=[]
var item = db.categoriesMP.findOne({_id:"Cell_Phones_and_Accessories"});
var criteria = '^'+item.path+item._id+',';
var children = db.categoriesMP.find({path: { $regex: criteria, $options: 'i' }});
while(true === children.hasNext()) {
var child = children.next();
descendants.push(child._id);
}


descendants.join(",")
//Cell_Phones_and_Smartphones,Headsets,Batteries,Cables_And_Adapters,Nokia,Samsung,Apple,HTC,Vyacheslav
29 changes: 29 additions & 0 deletions MaterializedPathReference_operating.js
@@ -0,0 +1,29 @@
use TreeMongo;

print("inserting")
var ancestorpath = db.categoriesMP.findOne({_id:'Electronics'}).path;
ancestorpath += 'Electronics,'
db.categoriesMP.insert({_id:'LG', path:ancestorpath});
//{ "_id" : "LG", "path" : "Electronics," }

db.categoriesMP.find({_id:'LG'})


print("updating/moving")

ancestorpath = db.categoriesMP.findOne({_id:'Cell_Phones_and_Smartphones'}).path;
ancestorpath +='Cell_Phones_and_Smartphones,'
db.categoriesMP.update({_id:'LG'},{$set:{path:ancestorpath}});
//{ "_id" : "LG", "path" : "Electronics,Cell_Phones_and_Accessories,Cell_Phones_and_Smartphones," }
db.categoriesMP.find({_id:'LG'});


//removing node
db.categoriesMP.remove({_id:'LG'});

//getting children
print ("getting children")
db.categoriesMP.find({$query:{path:'Electronics,'}})
//{ "_id" : "Cameras_and_Photography", "path" : "Electronics," }
//{ "_id" : "Shop_Top_Products", "path" : "Electronics," }
//{ "_id" : "Cell_Phones_and_Accessories", "path" : "Electronics," }
6 changes: 6 additions & 0 deletions MaterializedPathReference_pathtonode.js
@@ -0,0 +1,6 @@
use TreeMongo;

var path=[]
var item = db.categoriesMP.findOne({_id:"Nokia"})
print (item.path)
//Electronics,Cell_Phones_and_Accessories,Cell_Phones_and_Smartphones,
25 changes: 25 additions & 0 deletions NestedSetsParentReference.js
@@ -0,0 +1,25 @@
use TreeMongo;
db.categoriesNSO.drop();
db.categoriesNSO.insert({_id:"Electronics",parent:'', left:1, right:44});
db.categoriesNSO.insert({_id:"Cameras_and_Photography",parent:"Electronics", order:10, left:2, right:13});
db.categoriesNSO.insert({_id:"Digital_Cameras",parent:"Cameras_and_Photography", order:10, left:3, right:4});
db.categoriesNSO.insert({_id:"Camcorders",parent:"Cameras_and_Photography", order:20, left:5, right:6});
db.categoriesNSO.insert({_id:"Lenses_and_Filters",parent:"Cameras_and_Photography", order:30, left:7, right:8});
db.categoriesNSO.insert({_id:"Tripods_and_supports",parent:"Cameras_and_Photography", order:40, left:9, right:10});
db.categoriesNSO.insert({_id:"Lighting_and_studio",parent:"Cameras_and_Photography", order:50, left:11, right:12});
db.categoriesNSO.insert({_id:"Shop_Top_Products",parent:"Electronics", order:20, left:14, right:23});
db.categoriesNSO.insert({_id:"IPad",parent:"Shop_Top_Products", order:10, left:15, right:16});
db.categoriesNSO.insert({_id:"IPhone",parent:"Shop_Top_Products", order:20, left: 17, right:18});
db.categoriesNSO.insert({_id:"IPod",parent:"Shop_Top_Products", order:30, left:19, right:20});
db.categoriesNSO.insert({_id:"Blackberry",parent:"Shop_Top_Products", order:40, left:21, right:22});
db.categoriesNSO.insert({_id:"Cell_Phones_and_Accessories",parent:"Electronics", order:30, left:24, right:43});
db.categoriesNSO.insert({_id:"Cell_Phones_and_Smartphones",parent:"Cell_Phones_and_Accessories", order:10, left:25, right:36});
db.categoriesNSO.insert({_id:"Headsets",parent:"Cell_Phones_and_Accessories", order:20, left:37, right:38});
db.categoriesNSO.insert({_id:"Batteries",parent:"Cell_Phones_and_Accessories", order:30, left:39, right:40});
db.categoriesNSO.insert({_id:"Cables_And_Adapters",parent:"Cell_Phones_and_Accessories", order:40, left:41, right:42});
db.categoriesNSO.insert({_id:"Nokia",parent:"Cell_Phones_and_Smartphones", order:10, left:26, right:27});
db.categoriesNSO.insert({_id:"Samsung",parent:"Cell_Phones_and_Smartphones", order:20, left:28, right:29});
db.categoriesNSO.insert({_id:"Apple",parent:"Cell_Phones_and_Smartphones", order:30, left:30, right:31});
db.categoriesNSO.insert({_id:"HTC",parent:"Cell_Phones_and_Smartphones", order:40, left:32, right:33});
db.categoriesNSO.insert({_id:"Vyacheslav",parent:"Cell_Phones_and_Smartphones", order:50, left:34, right:35});

12 changes: 12 additions & 0 deletions NestedSetsParentReference_childsordered.js
@@ -0,0 +1,12 @@
use TreeMongo;
db.categoriesNSO.find({parent:"Electronics"}).sort({order:1});

exit
/*
{ "_id" : "Cameras_and_Photography", "parent" : "Electronics", "order" : 10, "left" : 2, "right" : 13 }
{ "_id" : "Shop_Top_Products", "parent" : "Electronics", "order" : 20, "left" : 14, "right" : 23 }
{ "_id" : "LG", "left" : 24, "right" : 25, "parent" : "Electronics", "order" : 25 }
{ "_id" : "Cell_Phones_and_Accessories", "parent" : "Electronics", "order" : 30, "left" : 26, "right" : 45 }
*/
7 changes: 7 additions & 0 deletions NestedSetsParentReference_movesubtree.js
@@ -0,0 +1,7 @@
/**
* Created with JetBrains PhpStorm.
* User: V
* Date: 06.01.13
* Time: 10:49
* To change this template use File | Settings | File Templates.
*/
52 changes: 52 additions & 0 deletions NestedSetsParentReference_operating_addnewnode.js
@@ -0,0 +1,52 @@
use TreeMongo;

//assume, we want to add LG node under Electronics
//new node would have left value of 24, affecting all remaining left values according to traversal rules
// and will have right value of 25, affecting all remaining right values including root one.

//take next node in traversal tree
var followingsibling = db.categoriesNSO.findOne({_id:"Cell_Phones_and_Accessories"});
var previoussignling = db.categoriesNSO.findOne({_id:"Shop_Top_Products"});
var neworder = parseInt((followingsibling.order + previoussignling.order)/2);
//new node will have left value of the following sibling and right value - incremented by two following sibling's left one
var newnode = {_id:'LG', left:followingsibling.left,right:followingsibling.left+1, parent:followingsibling.parent, order:neworder};


//now we have to create the place for the new node
//update affects right values of all ancestor nodes
//3th and 4th parameters: false stands for upsert=false and true stands for multi=true
db.categoriesNSO.update({right:{$gt:followingsibling.right}},{$inc:{right:2}}, false, true)

//and affects all nodes that remain for traversal
db.categoriesNSO.update({left:{$gte:followingsibling.left}, right:{$lte:followingsibling.right}},{$inc:{left:2, right:2}}, false, true)

// ready to insert
db.categoriesNSO.insert(newnode)

exit

/*
+--Electronics (1,46) ord.[undefined]
+----Cameras_and_Photography (2,13) ord.[10]
+-------Digital_Cameras (3,4) ord.[10]
+-------Camcorders (5,6) ord.[20]
+-------Lenses_and_Filters (7,8) ord.[30]
+-------Tripods_and_supports (9,10) ord.[40]
+-------Lighting_and_studio (11,12) ord.[50]
+-----Shop_Top_Products (14,23) ord.[20]
+-------IPad (15,16) ord.[10]
+-------IPhone (17,18) ord.[20]
+-------IPod (19,20) ord.[30]
+-------Blackberry (21,22) ord.[40]
+-----LG (24,25) ord.[25]
+-----Cell_Phones_and_Accessories (26,45) ord.[30]
+-------Cell_Phones_and_Smartphones (27,38) ord.[10]
+----------Nokia (28,29) ord.[10]
+----------Samsung (30,31) ord.[20]
+----------Apple (32,33) ord.[30]
+----------HTC (34,35) ord.[40]
+----------Vyacheslav (36,37) ord.[50]
+--------Headsets (39,40) ord.[20]
+--------Batteries (41,42) ord.[30]
+--------Cables_And_Adapters (43,44) ord.[40]
*/
25 changes: 25 additions & 0 deletions NestedSetsReference.js
@@ -0,0 +1,25 @@
use TreeMongo;
db.categoriesNSO.drop();
db.categoriesNSO.insert({_id:"Electronics",parent:'', left:1, right:44});
db.categoriesNSO.insert({_id:"Cameras_and_Photography",parent:"Electronics", order:10, left:2, right:13});
db.categoriesNSO.insert({_id:"Digital_Cameras",parent:"Cameras_and_Photography", order:10, left:3, right:4});
db.categoriesNSO.insert({_id:"Camcorders",parent:"Cameras_and_Photography", order:20, left:5, right:6});
db.categoriesNSO.insert({_id:"Lenses_and_Filters",parent:"Cameras_and_Photography", order:30, left:7, right:8});
db.categoriesNSO.insert({_id:"Tripods_and_supports",parent:"Cameras_and_Photography", order:40, left:9, right:10});
db.categoriesNSO.insert({_id:"Lighting_and_studio",parent:"Cameras_and_Photography", order:50, left:11, right:12});
db.categoriesNSO.insert({_id:"Shop_Top_Products",parent:"Electronics", order:20, left:14, right:23});
db.categoriesNSO.insert({_id:"IPad",parent:"Shop_Top_Products", order:10, left:15, right:16});
db.categoriesNSO.insert({_id:"IPhone",parent:"Shop_Top_Products", order:20, left: 17, right:18});
db.categoriesNSO.insert({_id:"IPod",parent:"Shop_Top_Products", order:30, left:19, right:20});
db.categoriesNSO.insert({_id:"Blackberry",parent:"Shop_Top_Products", order:40, left:21, right:22});
db.categoriesNSO.insert({_id:"Cell_Phones_and_Accessories",parent:"Electronics", order:30, left:24, right:43});
db.categoriesNSO.insert({_id:"Cell_Phones_and_Smartphones",parent:"Cell_Phones_and_Accessories", order:10, left:25, right:36});
db.categoriesNSO.insert({_id:"Headsets",parent:"Cell_Phones_and_Accessories", order:20, left:37, right:38});
db.categoriesNSO.insert({_id:"Batteries",parent:"Cell_Phones_and_Accessories", order:30, left:39, right:40});
db.categoriesNSO.insert({_id:"Cables_And_Adapters",parent:"Cell_Phones_and_Accessories", order:40, left:41, right:42});
db.categoriesNSO.insert({_id:"Nokia",parent:"Cell_Phones_and_Smartphones", order:10, left:26, right:27});
db.categoriesNSO.insert({_id:"Samsung",parent:"Cell_Phones_and_Smartphones", order:20, left:28, right:29});
db.categoriesNSO.insert({_id:"Apple",parent:"Cell_Phones_and_Smartphones", order:30, left:30, right:31});
db.categoriesNSO.insert({_id:"HTC",parent:"Cell_Phones_and_Smartphones", order:40, left:32, right:33});
db.categoriesNSO.insert({_id:"Vyacheslav",parent:"Cell_Phones_and_Smartphones", order:50, left:34, right:35});

81 changes: 81 additions & 0 deletions NestedSetsReference_movenode.js
@@ -0,0 +1,81 @@
use TreeMongo;

//assume, we want to move LG(24,25) with parent Electronics(1,46) to new location with a parent
// Cell_Phones_and_Smartphones as a last child

//Step 1 - remove LG node from tree
// see noderemoval example

//Step2 take right value of the new parent
var newparent = db.categoriesNSO.findOne({_id:"Cell_Phones_and_Smartphones"});
//new node will have left value of the parent's right value and right value - incremented by one parent's right one
var nodetomove = {_id:'LG', left:newparent.right,right:newparent.right+1, parent:newparent._id}


//now we have to create the place for the new node
//update affects right values of all nodes on a further traversal path
//3th and 4th parameters: false stands for upsert=false and true stands for multi=true
db.categoriesNSO.update({right:{$gte:newparent.right}},{$inc:{right:2}}, false, true)

//and affects all nodes that remain for traversal
db.categoriesNSO.update({left:{$gte:newparent.right}},{$inc:{left:2}}, false, true)

// ready to insert
db.categoriesNSO.insert(nodetomove)
nodetomove

exit

/*
After Step1 removal LG: node:
+-Electronics (1,44)
+--Cameras_and_Photography (2,13)
+-----Digital_Cameras (3,4)
+-----Camcorders (5,6)
+-----Lenses_and_Filters (7,8)
+-----Tripods_and_supports (9,10)
+-----Lighting_and_studio (11,12)
+---Shop_Top_Products (14,23)
+-----IPad (15,16)
+-----IPhone (17,18)
+-----IPod (19,20)
+-----Blackberry (21,22)
+---Cell_Phones_and_Accessories (24,43)
+-----Cell_Phones_and_Smartphones (25,36)
+--------Nokia (26,27)
+--------Samsung (28,29)
+--------Apple (30,31)
+--------HTC (32,33)
+--------Vyacheslav (34,35)
+------Headsets (37,38)
+------Batteries (39,40)
+------Cables_And_Adapters (41,42)
After step 2
+-Electronics (1,46)
+--Cameras_and_Photography (2,13)
+-----Digital_Cameras (3,4)
+-----Camcorders (5,6)
+-----Lenses_and_Filters (7,8)
+-----Tripods_and_supports (9,10)
+-----Lighting_and_studio (11,12)
+---Shop_Top_Products (14,23)
+-----IPad (15,16)
+-----IPhone (17,18)
+-----IPod (19,20)
+-----Blackberry (21,22)
+---Cell_Phones_and_Accessories (24,45)
+-----Cell_Phones_and_Smartphones (25,38)
+---------Nokia (26,27)
+---------Samsung (28,29)
+---------Apple (30,31)
+---------HTC (32,33)
+---------Vyacheslav (34,35)
+---------LG (36,37)
+-------Headsets (39,40)
+-------Batteries (41,42)
+-------Cables_And_Adapters (43,44)
*/
14 changes: 14 additions & 0 deletions NestedSetsReference_nodedescendants.js
@@ -0,0 +1,14 @@
use TreeMongo;

var descendants=[]
var item = db.categoriesNSO.findOne({_id:"Cell_Phones_and_Accessories"});
print ('('+item.left+','+item.right+')')
var children = db.categoriesNSO.find({left:{$gt:item.left}, right:{$lt:item.right}});
while(true === children.hasNext()) {
var child = children.next();
descendants.push(child._id);
}


descendants.join(",")
//Cell_Phones_and_Smartphones,Headsets,Batteries,Cables_And_Adapters,Nokia,Samsung,Apple,HTC,Vyacheslav
49 changes: 49 additions & 0 deletions NestedSetsReference_operating_addnewnode.js
@@ -0,0 +1,49 @@
use TreeMongo;

//assume, we want to add LG node under Electronics
//new node would have left value of 24, affecting all remaining left values according to traversal rules
// and will have right value of 25, affecting all remaining right values including root one.

//take next node in traversal tree
var followingsibling = db.categoriesNSO.findOne({_id:"Cell_Phones_and_Accessories"});
//new node will have left value of the following sibling and right value - incremented by two following sibling's left one
var newnode = {_id:'LG', left:followingsibling.left,right:followingsibling.left+1, parent:followingsibling.parent}


//now we have to create the place for the new node
//update affects right values of all ancestor nodes
//3th and 4th parameters: false stands for upsert=false and true stands for multi=true
db.categoriesNSO.update({right:{$gt:followingsibling.right}},{$inc:{right:2}}, false, true)

//and affects all nodes that remain for traversal
db.categoriesNSO.update({left:{$gte:followingsibling.left}, right:{$lte:followingsibling.right}},{$inc:{left:2, right:2}}, false, true)

// ready to insert
db.categoriesNSO.insert(newnode)

exit

/*
+-Electronics (1,46)
+---Cameras_and_Photography (2,13)
+------Digital_Cameras (3,4)
+------Camcorders (5,6)
+------Lenses_and_Filters (7,8)
+------Tripods_and_supports (9,10)
+------Lighting_and_studio (11,12)
+----Shop_Top_Products (14,23)
+------IPad (15,16)
+------IPhone (17,18)
+------IPod (19,20)
+------Blackberry (21,22)
+----LG (24,25)
+----Cell_Phones_and_Accessories (26,45)
+------Cell_Phones_and_Smartphones (27,38)
+---------Nokia (28,29)
+---------Samsung (30,31)
+---------Apple (32,33)
+---------HTC (34,35)
+---------Vyacheslav (36,37)
+-------Headsets (39,40)
+-------Batteries (41,42)
+-------Cables_And_Adapters (43,44) */

0 comments on commit abc9e0e

Please sign in to comment.