Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
mongotree
Advanced tools
Readme
Manage tree structures in MongoDB
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
Manage tree structures in MongoDB
The npm package mongotree receives a total of 4 weekly downloads. As such, mongotree popularity was classified as not popular.
We found that mongotree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.