首页 > 代码库 > 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. 内容修改很低效适合静态树。

MongoDB五种树形结构表示法