
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
mongoose-tree-ancestors
Advanced tools
Mongoose Tree Structure with an Array of Ancestors plugin for mongoose
Do you ever wanted to integrate a Tree Structure with documents refering to a parent, and the parent of the parent, avoiding a recursive function? Probably not, but if you ever think about that, you will be loosing a lot of time.
Instead of that, you can apply a data structure called ** Mongoose Tree Structure with an Array of Ancestors ** . What's that?
Take a look in the official Mongoose Documentation clicking here to know more about that.
It keeps track of all the changes for you. It's hooked to all the mongoose operations (update, findOneAndUpdate, save, remove and findOneAndRemove) you do through your model.
Everytime you do an operation over a document, this module will keep updated the array of ancestors for you.
Now, let's see how to use it...
npm install mongoose-tree-ancestors --save
Just include the call to this module in the same place you declare your Mongoose Schema:
'use strict';
const mongoose = require('mongoose');
const mongooseTreeAncestors = require('mongoose-tree-ancestors');
// Declare a Model name
const modelName = 'product_category';
// Prepare the Schema
const schema = new mongoose.Schema({
// Not required, but it's useful to keep awareness of this field
parent: {
type: mongoose.Schema.Types.ObjectId,
ref: 'group'
},
// Not required, but it's useful to keep awareness of this field
ancestors: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'group'
}],
categoryName: String,
createdAt: {
type: Date,
default: Date.now
},
updatedAt: Date
}, {
collection: 'products_categories',
timestamps: true
});
// Add the mongooseTreeAncestors Plugin to the schema
mongooseTreeAncestors(schema, {
// Set the parent field name and model reference
parentFieldName: 'parent',
parentFieldRefModel: modelName,
// Set the ancestors field name and model reference
ancestorsFieldName: 'ancestors',
ancestorsFieldRefModel: modelName
});
module.exports = mongoose.model(modelName, schema);
After that, you will have implemented an automated model to handle the data in the ancestor
's field.
If you already have a parent
field, you can call the status method buildAncestors
to build (or rebuild) all your ancestor's structure
yourModel.buildAncestors();
FAQs
Mongoose Tree Structure with an Array of Ancestors plugin for mongoose
The npm package mongoose-tree-ancestors receives a total of 30 weekly downloads. As such, mongoose-tree-ancestors popularity was classified as not popular.
We found that mongoose-tree-ancestors 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.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.