• 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. 内容修改很低效适合静态树。

    -----------------------------------------------------------------------
    Simple is Beautiful,Less is More.
    --FuGardenia
     
    分类
  • 相关阅读:
    求1+2+3+...+n
    孩子们的游戏(圆圈中最后剩下的数) 约瑟夫环
    扑克牌顺子
    翻转单词顺序列
    左旋转字符串
    和为S的两个数字
    和为S的连续正数序列
    CocoaPods 更新
    UITextView 动态高度计算(iOS7版)
    Mac 把图片反色
  • 原文地址:https://www.cnblogs.com/Don/p/6079312.html
Copyright © 2020-2023  润新知