• MongoDB
  • < 1

Managing JSON Taxonomy with Laravel and MongoDB

  • POSTED ON
  • February 17, 2017
  • POSTED BY
  • Aayan Arif
  • POSTED ON February 17, 2017
  • POSTED BY Aayan Arif

Managing a preformatted taxonomy (JSON Data) in a tree format with nth level nodes has never been easy, especially with MongoDB. Although there is no rocket science involved, while manipulating a nested array or tree in MongoDB having limited (up to 2) levels of nodes. Moreover, manipulation of multilevel trees requires some additional code implementations....

Managing a preformatted taxonomy (JSON Data) in a tree format with nth level nodes has never been easy, especially with MongoDB. Although there is no rocket science involved, while manipulating a nested array or tree in MongoDB having limited (up to 2) levels of nodes. Moreover, manipulation of multilevel trees requires some additional code implementations.

Objectives

vteams target was to achieve the following goals:

  1. List all super categories, categories and sub categories
  2. Insert new super category, category and sub category
  3. Update existing super category, category and sub category
  4. Delete existing super category, category and sub category
  5. Get the path to a node (in order to build the breadcrumb section, for example)
  6. Get all node descendants (e.g. to be able to select sub categories from a category
    .

The Challenge

MongoDB’s positional operator $ can not be used in nested array, documents having more than 2 levels of nodes could be a challenge, where the number of nodes at any level varies dynamically.

Solution

vteams overcame this issue by splitting each and every level into several areas for easy management. Whereby, on every page, a single level can update, delete, add new nodes to itself and can view all ‘child’ files related to itself and so on for all other sub levels.

While updating a specific document in mongoDB during collection. vteams accomplished this through the following steps:

  • Select the appropriate document
  • Iterate through the node up to nth level
  • Find the specific node to be updated or deleted
  • Modify this node accordingly
  • Reconstruct the array with updated data
  • Merge newly formed array with existing one
  • Finally update the collection
    .

Doing this vteams also tracked the path for each node level, which made it easy to implement breadcrumb (for levels) feature.

ABOUT THE AUTHOR

Aayan Arif

Content Strategist at vteams - Aayan has over 8 years of experience of working with multiple industries.

0 Comments

Leave a Reply

More Related Article
We provide tips and advice on delivering excellent customer service, engaging your customers, and building a customer-centric business.