Socket
Socket
Sign inDemoInstall

mongotree

Package Overview
Dependencies
1
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mongotree

Manage tree structures in MongoDB


Version published
Weekly downloads
4
Maintainers
1
Install size
89.1 kB
Created
Weekly downloads
 

Readme

Source

mongotree

Manage tree structures in MongoDB

Getting Started

Install the module with: npm install mongotree

Create a default tree

var mongotree = require('mongotree'),
	MongoClient = require('mongodb').MongoClient,
	dsn = 'mongodb://localhost:27017/my-database';

// Create a new tree structure in a mongodb collection
MongoClient.connect(dsn, function(err, db) {
	mongotree.addTree('products/cameras/accessories', db.collection('my-collection'), function(err, tree) {
		console.log(tree);
		db.close();
	});
});

// outputs
[ { _id: 'products', name: 'products', parent: '' },
  { _id: 'products/cameras', name: 'cameras', parent: 'products' },
  { _id: 'products/cameras/accessories',
    name: 'accessories',
    parent: 'products/cameras' } ]

Create a tree with custom params for nodes

var params = {
	path: 'products/cameras/accessories',
	created: new Date()
};
mongotree.addTree(params, db.collection('my-collection'), function(err, tree) {
	console.log(tree);
});

// outputs
[ { _id: 'products',
    name: 'products',
    parent: '',
    created: Fri Aug 16 2013 09:41:44 GMT-0700 (MST) },
  { _id: 'products/cameras',
    name: 'cameras',
    parent: 'products',
    created: Fri Aug 16 2013 09:41:44 GMT-0700 (MST) },
  { _id: 'products/cameras/accessories',
    name: 'accessories',
    parent: 'products/cameras',
    created: Fri Aug 16 2013 09:41:44 GMT-0700 (MST) } ]

Create a tree with friendly path names

mongotree.addTree('Products / Cameras / Camera Accessories', collection, function(err, tree) {
	console.log(tree);
});

// outputs
[ { _id: 'products',
    name: 'Products',
    parent: '' },
  { _id: 'products/cameras',
    name: 'Cameras',
    parent: 'products' },
  { _id: 'products/cameras/camera-accessories',
    name: 'Camera Accessories',
    parent: 'products/cameras' } ]

Get ancestors

mongotree.getAncestors('products/cameras/accessories', collection, function(err, tree) {
	console.log(tree);
});

// outputs
[ { _id: 'products', name: 'products', parent: '' },
  { _id: 'products/cameras', name: 'cameras', parent: 'products' } ]

Get decendants

mongotree.getDecendants('products', collection, function(err, tree) {
	console.log(tree);
});

// outputs
[ { _id: 'products/cameras', name: 'cameras', parent: 'products' },
  { _id: 'products/cameras/accessories',
    name: 'accessories',
    parent: 'products/cameras' } ]

FAQs

Last updated on 17 Aug 2013

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc