D
D
Dmitry Kashkovsky2015-11-01 17:26:50
MongoDB
Dmitry Kashkovsky, 2015-11-01 17:26:50

How to display a menu in an online store?

Hello!
I am writing a bicycle (online store) for myself on node.js and mongodb (mongoose).
But there was a question on operation with a tree of categories.
Now there is a structure like this:

var CategorySchema = new Schema({
    name: String,
    slug: String,
    text: String,
    parentSlug: {type: String, default: ""},
    ancestry: {type:[String], default: []} // предки для получения полного урла категории и хлебных крошек
});

nesting will be 4-5 maximum.
Task: displaying the menu as on kosi-pili.ru (the menu itself is the 1st level of nesting, and when you hover over the mouse, 2 and 3 are displayed).
What is the best way to do this?
The disadvantage of this structure is that to collect the tree, recursion to the database is required, which is not suitable (the number of all sections is about 300 - a very large overhead). Yes, and asynchronous recursion is still fun ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2015-11-01
@lega

to collect the tree, recursion to the base is required

Keep a list of (all) ancestors in children, then no recursions are needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question