How do you design a schema for tree structures in MongoDB

0 votes
Can you tell me How do you design a schema for tree structures in MongoDB?
Feb 21 in Node-js by Ashutosh
• 20,870 points
44 views

1 answer to this question.

0 votes

Designing a schema for tree structures in MongoDB depends on how you need to traverse and query the data. Here are some common approaches:

1. Parent-Reference Model (Adjacency List)

Each node stores a reference to its parent.

Schema Example:

{

  "_id": ObjectId("1"),

  "name": "Node A",

  "parent": ObjectId("null")  // Root node (no parent)

}

{

  "_id": ObjectId("2"),

  "name": "Node B",

  "parent": ObjectId("1")  // Child of Node A

}

2. Child-Reference Model

Each parent node keeps an array of references to its children.

{

  "_id": ObjectId("1"),

  "name": "Node A",

  "children": [ObjectId("2"), ObjectId("3")]

}

3. Materialized Path

Each node stores the full path as a string.

{

  "_id": ObjectId("1"),

  "name": "Node A",

  "path": "1"

}

{

  "_id": ObjectId("2"),

  "name": "Node B",

  "path": "1/2"

}

4. Nested Set Model

Each node has left and right values representing its position in a pre-ordered traversal.

{

  "_id": ObjectId("1"),

  "name": "Node A",

  "left": 1,

  "right": 6

}

{

  "_id": ObjectId("2"),

  "name": "Node B",

  "left": 2,

  "right": 3

}

5. Closure Table Model (Hybrid Approach)

Stores parent-child relationships in a separate collection.

{

  "_id": ObjectId("1"),

  "ancestor": ObjectId("1"),

  "descendant": ObjectId("2"),

  "depth": 1

}

answered Feb 22 by Kavya

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer

What are MongoDB data types, and how do you define them in a schema?

MongoDB supports various data types, including: String (String) ...READ MORE

answered Feb 23 in Node-js by anonymous
86 views
0 votes
1 answer

How do you model a many-to-many relationship in MongoDB with an example?

In MongoDB, a many-to-many relationship can be ...READ MORE

answered Feb 23 in Node-js by Kavya
68 views
0 votes
1 answer

How do you handle concerns for write operations in MongoDB?

Write operations in MongoDB need to be ...READ MORE

answered Feb 23 in Node-js by Kavya
102 views
0 votes
1 answer

How can I implement user authentication with JWT in an Express.js app?

In an Express.js application, you can use ...READ MORE

answered Dec 17, 2024 in Java-Script by Navya
106 views
0 votes
1 answer

Is it possible to handle React events using the Chrome extension?

Yes, it's possible to handle React events ...READ MORE

answered Feb 22 in Node-js by Kavya
42 views
0 votes
1 answer

How can I use all the React events with Material-UI components?

The best approach is to leverage the ...READ MORE

answered Feb 22 in Node-js by Kavya
41 views
0 votes
1 answer

Why won't React events fire, or what could prevent them from firing?

If React events are not firing, several ...READ MORE

answered Feb 22 in Node-js by Kavya
47 views
0 votes
1 answer

How do you embed a document in MongoDB for better performance?

Embedding documents in MongoDB is a common ...READ MORE

answered Feb 22 in Node-js by Kavya
68 views
0 votes
1 answer

How would you model a one-to-many relationship in MongoDB?

In MongoDB, a one-to-many relationship can be ...READ MORE

answered Feb 22 in Node-js by Kavya
47 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP