Five tree-structured representations of MongoDB

Source: Internet
Author: User
Tags findone

Five tree-structured representations of MongoDB

First type: Parent link structure
Db. categories. Insert ({_ID: "MongoDB", Parent:"Databases"})Db. categories. Insert ({_ID: "DBM", Parent:"Databases"})Db. categories. Insert ({_ID: "Databases", Parent:"Programming"})Db.categories.insert ({ _id:  Languages ", Parent: " Programming "}) db.insert ({_id: " Programming ", Parent:  "Books"}) db.categories.insert ( {_id: Span class= "hljs-string" > "Books", Parent:null})          

Characteristics:

    1. Quick Get parent node:

      Db.categories.findOne ({_id: "MongoDB"}). Parent

    2. Easy to create parent node index

      Db.categories.ensureIndex ({parent:1})

    3. Get son node by querying parent node

      Db.categories.find ({parent: "Databases"})

4. Multiple queries are required to retrieve subtrees.

Second Type: sub-link structure
Db. categories. Insert ({_ID: "MongoDB", Children: []})Db. categories. Insert ({_ID: "DBM", Children: []})Db. categories. Insert ({_ID: "Databases", Children: ["MongoDB","dbm"]})Db. categories. Insert ({ _id: "Languages", Children: []})db. Categories. Insert ( { _id: " Programming ", Children: [ " Databases ", " Languages "]})db. Categories. Insert ( { _id:  "Books", Children: [ "Programming"]})          

Characteristics:

    1. Quick Get son node

      Db.categories.findOne ({_id: "Databases"}). Children

    2. Easy to create child node index

      Db.categories.ensureIndex ({children:1})

    3. Get parent node by querying son node

      Db.categories.find ({children: "MongoDB"})

    4. Suitable for storing storage graphs, one node may have multiple parents.

The third type: ancestor queue structure
Db. categories. Insert ({_ID: "MongoDB", Ancestors: ["Books","Programming","Databases"], Parent:"Databases"})Db. categories. Insert ({_ID: "dbm", Ancestors: ["Books","Programming","Databases"], Parent:"Databases"})Db. categories. Insert ({_ID: "Databases", Ancestors: ["Books","Programming"], Parent:"Programming"})Db. categories. Insert ({_ID:  "Languages", Ancestors: [ "Books", Span class= "hljs-string" > "programming"], Parent:  "Programming"})  Db.categories.insert ({_id: " Programming ", Ancestors: [ "Books"], Parent:  "Books"}) db.categories.insert ( {_id: Span class= "hljs-string" > "Books", Ancestors: [], parent:null})       / span>               

Characteristics:

    1. Get Ancestors Fast

      Db.categories.findOne ({_id: "MongoDB"}). Ancestors

    2. Easy to create ancestor node index

      Db.categories.ensureIndex ({ancestors:1})

    3. retrieving descendants by querying ancestors

      Db.categories.find ({ancestors: "Programming"})

    4. Ancestor mode is slightly slower than materialized path mode

Fourth type: materialized path structure
Db. categories. Insert ({_ID: "Books", Path:null})Db. categories. Insert ({_ID: "Programming", Path:", Books,"})Db. categories. Insert ({_ID: "Databases", Path:", Books,programming,"})Db. categories. Insert ({_id: Span class= "hljs-string" > "Languages", Path: db.categories.insert ( {_id: Span class= "hljs-string" > "MongoDB", Path: db.categories.insert ({ Span class= "Hljs-rule" >_id:  dbm ", Path: ", Books,programming,databases, "})        

Characteristics:

    1. Retrieving sorting by query

      Db.categories.find (). Sort ({path:1})

    2. Quick Find child nodes

      Db.categories.find ({path:/^,books,/}) Db.categories.find ({path:/^,books,/})

    3. To create an index for a path

      Db.categories.ensureIndex ({path:1})

Fifth: Set model

Db. categories. Insert ({_ID: "Books", Parent:0, Left:1, right:12})Db. categories. Insert ({_ID: "Programming", Parent:"Books", left:2, right:11})Db. categories. Insert ({_ID: "Languages", Parent:"Programming", left:3, right:4})Db. categories. Insert ({_ID: "Databases", Parent:"Programming", left:5, right: 10}) db. Categories. Insert ( { _id: "MongoDB", Parent: "Databases", left: 6, right: 7})db. Categories. Insert ( { _id: "dbm", Parent: "Databases", left: 8 , right: 9})                

Characteristics:

    1. Quickly get child nodes

      var databasecategory = Db.categories.findOne ({_id: "Databases"}); Db.categories.find ({left: {$gt: Databasecategory.left}, right: {$lt: Databasecategory.right}});

    2. Content modification is inefficient for static trees.

Five tree-structured representations of MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.