zoukankan      html  css  js  c++  java
  • MongoDB五种树形结构表示法

    MongoDB五种树形结构表示法

     

    MongoDB五种树形结构表示法

    第一种:父链接结构

    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.categories.insert( { _id: "Programming", parent: "Books" } )
    db.categories.insert( { _id: "Books", parent: null } )
    

    特征:

    1. 快速获取父节点:

      db.categories.findOne( { _id: "MongoDB" } ).parent

    2. 方便创建父节点索引

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

    3. 通过查询父节点获取儿子节点

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

    4.需要多个查询来检索子树。

    第二种:子链接结构

    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" ] } )
    

    特征:

    1. 快速获取儿子节点

      db.categories.findOne( { _id: "Databases" } ).children

    2. 方便创建子节点索引

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

    3. 通过查询儿子节点获取父节点

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

    4. 适合存储存储图,一个节点可能有多个父母。

    第三种:祖先队列结构

    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", "Programming" ], parent: "Programming" } )
    db.categories.insert( { _id: "Programming", ancestors: [ "Books" ], parent: "Books" } )
    db.categories.insert( { _id: "Books", ancestors: [ ], parent: null } )
    

    特征:

    1. 快速获取祖先

      db.categories.findOne( { _id: "MongoDB" } ).ancestors

    2. 方便创建祖先节点索引

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

    3. 通过查询祖先来获取后代

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

    4. 祖先模式略慢于物化路径模式

    第四种:物化路径结构

    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: "Languages", path: ",Books,Programming," } )
    db.categories.insert( { _id: "MongoDB", path: ",Books,Programming,Databases," } )
    db.categories.insert( { _id: "dbm", path: ",Books,Programming,Databases," } )
    

    特征:

    1. 通过查询检索排序

      db.categories.find().sort( { path: 1 } )

    2. 快速查找子节点

      db.categories.find( { path: /^,Books,/ } ) db.categories.find( { path: /^,Books,/ } )

    3. 为路径创建索引

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

    第五种:集合模型

    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 } )
    

    特征:

    1. 快速获取子节点

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

    2. 内容修改很低效适合静态树。

  • 相关阅读:
    bzoj 1176 cdq分治套树状数组
    Codeforces 669E cdq分治
    Codeforces 1101D 点分治
    Codeforces 1100E 拓扑排序
    Codeforces 1188D Make Equal DP
    Codeforces 1188A 构造
    Codeforces 1188B 式子转化
    Codeforces 1188C DP 鸽巢原理
    Codeforces 1179D 树形DP 斜率优化
    git commit -m "XX"报错 pre -commit hook failed (add --no-verify to bypass)问题
  • 原文地址:https://www.cnblogs.com/HuiLove/p/3962592.html
Copyright © 2011-2022 走看看