Click here to Skip to main content
12,819,248 members (32,121 online)
Click here to Skip to main content
Articles » Database » NoSQL » General » Downloads

Stats

38.9K views
579 downloads
22 bookmarked
Posted 4 Jan 2013

Storing Tree like Hierarchy Structures With MongoDB

, 16 Jan 2013 CC (ASA 3U)
This article demonstrates three approaches for storing tree like structures with NoSQL databases on example of the MongoDB.
use TreeMongo;

print("inserting")
var ancestorpath = db.categoriesAAO.findOne({_id:'Electronics'}).ancestors;
ancestorpath.push('Electronics')
db.categoriesAAO.insert({_id:'LG', parent:'Electronics',ancestors:ancestorpath});
//{ "_id" : "LG", "parent" : "Electronics", "ancestors" : [ "Electronics" ] }

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


print("updating/moving")

ancestorpath = db.categoriesAAO.findOne({_id:'Cell_Phones_and_Smartphones'}).ancestors;
ancestorpath.push('Cell_Phones_and_Smartphones')
db.categoriesAAO.update({_id:'LG'},{$set:{parent:'Cell_Phones_and_Smartphones', ancestors:ancestorpath}});
//{ "_id" : "LG", "ancestors" : [ 	"Electronics", 	"Cell_Phones_and_Accessories", 	"Cell_Phones_and_Smartphones" ], "parent" : "Cell_Phones_and_Smartphones" }
db.categoriesAAO.find({_id:'LG'});


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

//getting children
print ("getting children")
db.categoriesAAO.find({$query:{parent:'Electronics'}})

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

Share

About the Author

Vyacheslav Voronenko
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

You may also be interested in...

Pro
Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 16 Jan 2013
Article Copyright 2013 by Vyacheslav Voronenko
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid