Click here to Skip to main content
15,882,152 members
Articles / Database Development / NoSQL

Storing Tree like Hierarchy Structures With MongoDB, Part 2

Rate me:
Please Sign up or sign in to vote.
4.43/5 (5 votes)
6 Jan 2013CC (ASA 3U)6 min read 32.4K   296   8  
Three approaches to store tree like structures with NoSQL databases
This article demonstrates three approaches for storing tree like structures with NoSQL databases on example of the MongoDB.
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) */

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License


Written By
Web Developer
Ukraine Ukraine
Web Developer, interested in bleeding age web technologies and projects.

Experienced and interested in:
- High load web projects, bespoke software development
- DevOps: Chef, Ansible, Vagrant
- NoSQL (mongodb)
- Client stack (javascript core, jquery, AngularJS, HTML5 apis)
- *AAS (Amazon beanstalk, Redhat openshift)
- MEAN & Pure JS stack (Javascript, AngularJS, Node.JS, MongoDB)


-> DevOps inquiries
-> Other inquiries
-> Follow me on Github

Comments and Discussions