• 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
     
    分类
  • 相关阅读:
    从标准输入读取一行字符串的方法
    输入文件包含若干行,不知何时结束时,如何读入数据
    centos7.5 + docker + mysql5.7 多实例安装
    copula函数及其Var计算的Python实现
    让网络机器人看起来像人类用户
    流畅的python读书笔记
    神经网络层数和神经元数目 的一些建议
    SVM算法Python实现
    预测性维护{维修}又称:预知性、预见性维护{维修})
    WebDriver API 元素定位(Python)
  • 原文地址:https://www.cnblogs.com/Don/p/6079312.html
Copyright © 2020-2023  润新知