db-tutorial/pages/88c7d3/index.html

404 lines
96 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>MongoDB 建模示例 | DB-TUTORIAL</title>
<meta name="generator" content="VuePress 1.9.9">
<link rel="icon" href="/db-tutorial/img/favicon.ico">
<script src="https://cdn.wwads.cn/js/makemoney.js" type="text/javascript"></script>
<meta name="description" content="☕ db-tutorial 是一个数据库教程。">
<meta name="keywords" content="vuepress,theme,blog,vdoing">
<meta name="theme-color" content="#11a8cd">
<meta name="wwads-cn-verify" content="mxqWx62nfQQ9ocT4e5DzISHzOWyF4s">
<link rel="preload" href="/db-tutorial/assets/css/0.styles.51390d19.css" as="style"><link rel="preload" href="/db-tutorial/assets/js/app.be3f2e92.js" as="script"><link rel="preload" href="/db-tutorial/assets/js/2.aded268b.js" as="script"><link rel="preload" href="/db-tutorial/assets/js/38.5024023f.js" as="script"><link rel="prefetch" href="/db-tutorial/assets/js/10.551ab278.js"><link rel="prefetch" href="/db-tutorial/assets/js/11.c049b6a2.js"><link rel="prefetch" href="/db-tutorial/assets/js/12.92d587d6.js"><link rel="prefetch" href="/db-tutorial/assets/js/13.a7b35fa5.js"><link rel="prefetch" href="/db-tutorial/assets/js/14.9ec959b5.js"><link rel="prefetch" href="/db-tutorial/assets/js/15.e7943372.js"><link rel="prefetch" href="/db-tutorial/assets/js/16.c6ad7b26.js"><link rel="prefetch" href="/db-tutorial/assets/js/17.39500ebd.js"><link rel="prefetch" href="/db-tutorial/assets/js/18.29949979.js"><link rel="prefetch" href="/db-tutorial/assets/js/19.78f879b8.js"><link rel="prefetch" href="/db-tutorial/assets/js/20.3ce14df7.js"><link rel="prefetch" href="/db-tutorial/assets/js/21.9d311c7c.js"><link rel="prefetch" href="/db-tutorial/assets/js/22.90234550.js"><link rel="prefetch" href="/db-tutorial/assets/js/23.12cf9e2d.js"><link rel="prefetch" href="/db-tutorial/assets/js/24.5b436e46.js"><link rel="prefetch" href="/db-tutorial/assets/js/25.8f2dc7d9.js"><link rel="prefetch" href="/db-tutorial/assets/js/26.61665ff4.js"><link rel="prefetch" href="/db-tutorial/assets/js/27.dfc7cc88.js"><link rel="prefetch" href="/db-tutorial/assets/js/28.a6286a66.js"><link rel="prefetch" href="/db-tutorial/assets/js/29.bfa8c106.js"><link rel="prefetch" href="/db-tutorial/assets/js/3.82108019.js"><link rel="prefetch" href="/db-tutorial/assets/js/30.c93b7a2e.js"><link rel="prefetch" href="/db-tutorial/assets/js/31.d48aab81.js"><link rel="prefetch" href="/db-tutorial/assets/js/32.33f8bb7c.js"><link rel="prefetch" href="/db-tutorial/assets/js/33.eb3b622d.js"><link rel="prefetch" href="/db-tutorial/assets/js/34.1ba1a06c.js"><link rel="prefetch" href="/db-tutorial/assets/js/35.24e4fee8.js"><link rel="prefetch" href="/db-tutorial/assets/js/36.dc61504e.js"><link rel="prefetch" href="/db-tutorial/assets/js/37.f1c9c729.js"><link rel="prefetch" href="/db-tutorial/assets/js/39.30ed016e.js"><link rel="prefetch" href="/db-tutorial/assets/js/4.884deeca.js"><link rel="prefetch" href="/db-tutorial/assets/js/40.6a6327f0.js"><link rel="prefetch" href="/db-tutorial/assets/js/41.3767f4d8.js"><link rel="prefetch" href="/db-tutorial/assets/js/42.14a20b0f.js"><link rel="prefetch" href="/db-tutorial/assets/js/43.b059081c.js"><link rel="prefetch" href="/db-tutorial/assets/js/44.73f724d9.js"><link rel="prefetch" href="/db-tutorial/assets/js/45.68f2ab27.js"><link rel="prefetch" href="/db-tutorial/assets/js/46.aed8a61f.js"><link rel="prefetch" href="/db-tutorial/assets/js/47.3bbf2366.js"><link rel="prefetch" href="/db-tutorial/assets/js/48.81688356.js"><link rel="prefetch" href="/db-tutorial/assets/js/49.1d366c93.js"><link rel="prefetch" href="/db-tutorial/assets/js/5.42f2cdb7.js"><link rel="prefetch" href="/db-tutorial/assets/js/50.ee6f08b7.js"><link rel="prefetch" href="/db-tutorial/assets/js/51.47835d9d.js"><link rel="prefetch" href="/db-tutorial/assets/js/52.b0cab79c.js"><link rel="prefetch" href="/db-tutorial/assets/js/53.106440a2.js"><link rel="prefetch" href="/db-tutorial/assets/js/54.340ab690.js"><link rel="prefetch" href="/db-tutorial/assets/js/55.00623de9.js"><link rel="prefetch" href="/db-tutorial/assets/js/56.9f086601.js"><link rel="prefetch" href="/db-tutorial/assets/js/57.f5ad5f3f.js"><link rel="prefetch" href="/db-tutorial/assets/js/58.14827368.js"><link rel="prefetch" href="/db-tutorial/assets/js/59.c41f57d6.js"><link rel="prefetch" href="/db-tutorial/assets/js/6.5a5b4e54.js"><link rel="prefetch" href="/db-tutorial/assets/js/60.314534c0.js"><link rel="prefetch" href="/db-tutorial/assets/js/61.1ea5e4b7.js"><link rel="prefetch" href="/db-tutorial/assets/js/62.8b52e8fc.js"><link rel="prefetch" href="/db-tutorial/assets/js/63.d2fa8325.js"><link rel="prefetch" href="/db-tutorial/assets/js/64.ea2577e7.js"><link rel="prefetch" href="/db-tutorial/assets/js/65.563da2bb.js"><link rel="prefetch" href="/db-tutorial/assets/js/66.34eb51bd.js"><link rel="prefetch" href="/db-tutorial/assets/js/67.ab57f04f.js"><link rel="prefetch" href="/db-tutorial/assets/js/68.15b6f540.js"><link rel="prefetch" href="/db-tutorial/assets/js/69.54590de4.js"><link rel="prefetch" href="/db-tutorial/assets/js/7.913bec54.js"><link rel="prefetch" href="/db-tutorial/assets/js/70.40a2cea2.js"><link rel="prefetch" href="/db-tutorial/assets/js/71.3ce50922.js"><link rel="prefetch" href="/db-tutorial/assets/js/72.b9c022e9.js"><link rel="prefetch" href="/db-tutorial/assets/js/73.fba94661.js"><link rel="prefetch" href="/db-tutorial/assets/js/74.998d6c2f.js"><link rel="prefetch" href="/db-tutorial/assets/js/75.6efb68b0.js"><link rel="prefetch" href="/db-tutorial/assets/js/76.57273256.js"><link rel="prefetch" href="/db-tutorial/assets/js/77.3ddffb5b.js"><link rel="prefetch" href="/db-tutorial/assets/js/78.8fde3d74.js"><link rel="prefetch" href="/db-tutorial/assets/js/79.7a472c31.js"><link rel="prefetch" href="/db-tutorial/assets/js/8.103b4774.js"><link rel="prefetch" href="/db-tutorial/assets/js/80.4c55c65f.js"><link rel="prefetch" href="/db-tutorial/assets/js/81.cbdb67b8.js"><link rel="prefetch" href="/db-tutorial/assets/js/82.9c14d852.js"><link rel="prefetch" href="/db-tutorial/assets/js/83.a3d7d272.js"><link rel="prefetch" href="/db-tutorial/assets/js/84.6994dacc.js"><link rel="prefetch" href="/db-tutorial/assets/js/85.aca8d788.js"><link rel="prefetch" href="/db-tutorial/assets/js/86.00671865.js"><link rel="prefetch" href="/db-tutorial/assets/js/87.9076c4e8.js"><link rel="prefetch" href="/db-tutorial/assets/js/88.bbe68dab.js"><link rel="prefetch" href="/db-tutorial/assets/js/89.e2173071.js"><link rel="prefetch" href="/db-tutorial/assets/js/9.386bfe3a.js"><link rel="prefetch" href="/db-tutorial/assets/js/90.429162f2.js"><link rel="prefetch" href="/db-tutorial/assets/js/91.6ecc7c85.js"><link rel="prefetch" href="/db-tutorial/assets/js/92.efe42934.js"><link rel="prefetch" href="/db-tutorial/assets/js/93.69562766.js"><link rel="prefetch" href="/db-tutorial/assets/js/94.04aebfbc.js"><link rel="prefetch" href="/db-tutorial/assets/js/95.fe7cfce4.js"><link rel="prefetch" href="/db-tutorial/assets/js/96.674475d6.js"><link rel="prefetch" href="/db-tutorial/assets/js/97.8a7b73f2.js"><link rel="prefetch" href="/db-tutorial/assets/js/98.bf85add1.js">
<link rel="stylesheet" href="/db-tutorial/assets/css/0.styles.51390d19.css">
</head>
<body class="theme-mode-light">
<div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/db-tutorial/" class="home-link router-link-active"><img src="https://raw.githubusercontent.com/dunwu/images/master/common/dunwu-logo.png" alt="DB-TUTORIAL" class="logo"> <span class="site-name can-hide">DB-TUTORIAL</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/db-tutorial/12.数据库/01.数据库综合/" class="nav-link">数据库综合</a></div><div class="nav-item"><a href="/db-tutorial/12.数据库/02.数据库中间件/" class="nav-link">数据库中间件</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关系型数据库" class="dropdown-title"><a href="/db-tutorial/12.数据库/03.关系型数据库/" class="link-title">关系型数据库</a> <span class="title" style="display:none;">关系型数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/01.综合/" class="nav-link">综合</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/02.Mysql/" class="nav-link">Mysql</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/99.其他/" class="nav-link">其他</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档数据库" class="dropdown-title"><!----> <span class="title" style="display:;">文档数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/04.文档数据库/01.MongoDB/" class="nav-link">MongoDB</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="KV数据库" class="dropdown-title"><!----> <span class="title" style="display:;">KV数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/05.KV数据库/01.Redis/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="搜索引擎数据库" class="dropdown-title"><!----> <span class="title" style="display:;">搜索引擎数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/01.Elasticsearch/" class="nav-link">Elasticsearch</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/02.Elastic/" class="nav-link">Elastic技术栈</a></li></ul></div></div> <a href="https://github.com/dunwu/db-tutorial" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><!----> <nav class="nav-links"><div class="nav-item"><a href="/db-tutorial/12.数据库/01.数据库综合/" class="nav-link">数据库综合</a></div><div class="nav-item"><a href="/db-tutorial/12.数据库/02.数据库中间件/" class="nav-link">数据库中间件</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="关系型数据库" class="dropdown-title"><a href="/db-tutorial/12.数据库/03.关系型数据库/" class="link-title">关系型数据库</a> <span class="title" style="display:none;">关系型数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/01.综合/" class="nav-link">综合</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/02.Mysql/" class="nav-link">Mysql</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/03.关系型数据库/99.其他/" class="nav-link">其他</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文档数据库" class="dropdown-title"><!----> <span class="title" style="display:;">文档数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/04.文档数据库/01.MongoDB/" class="nav-link">MongoDB</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="KV数据库" class="dropdown-title"><!----> <span class="title" style="display:;">KV数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/05.KV数据库/01.Redis/" class="nav-link">Redis</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="搜索引擎数据库" class="dropdown-title"><!----> <span class="title" style="display:;">搜索引擎数据库</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/01.Elasticsearch/" class="nav-link">Elasticsearch</a></li><li class="dropdown-item"><!----> <a href="/db-tutorial/12.数据库/07.搜索引擎数据库/02.Elastic/" class="nav-link">Elastic技术栈</a></li></ul></div></div> <a href="https://github.com/dunwu/db-tutorial" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库综合</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>关系型数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>文档数据库</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>MongoDB</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/db-tutorial/pages/3288f3/" class="sidebar-link">MongoDB 应用指南</a></li><li><a href="/db-tutorial/pages/7efbac/" class="sidebar-link">MongoDB 的 CRUD 操作</a></li><li><a href="/db-tutorial/pages/75daa5/" class="sidebar-link">MongoDB 的聚合操作</a></li><li><a href="/db-tutorial/pages/4574fe/" class="sidebar-link">MongoDB 事务</a></li><li><a href="/db-tutorial/pages/562f99/" class="sidebar-link">MongoDB 建模</a></li><li><a href="/db-tutorial/pages/88c7d3/" aria-current="page" class="active sidebar-link">MongoDB 建模示例</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/88c7d3/#关系型模型" class="sidebar-link">关系型模型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#嵌入式文档一对一关系模型" class="sidebar-link">嵌入式文档一对一关系模型</a></li><li class="sidebar-sub-header level4"><a href="/db-tutorial/pages/88c7d3/#嵌入式文档一对一关系模型-嵌入式文档模式" class="sidebar-link">嵌入式文档一对一关系模型 - 嵌入式文档模式</a></li><li class="sidebar-sub-header level4"><a href="/db-tutorial/pages/88c7d3/#嵌入式文档一对一关系模型-子集模式" class="sidebar-link">嵌入式文档一对一关系模型 - 子集模式</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#嵌入式文档一对多关系模型" class="sidebar-link">嵌入式文档一对多关系模型</a></li><li class="sidebar-sub-header level4"><a href="/db-tutorial/pages/88c7d3/#嵌入式文档一对多关系模型-嵌入式文档模式" class="sidebar-link">嵌入式文档一对多关系模型 - 嵌入式文档模式</a></li><li class="sidebar-sub-header level4"><a href="/db-tutorial/pages/88c7d3/#嵌入式文档一对多关系模型-子集模式" class="sidebar-link">嵌入式文档一对多关系模型 - 子集模式</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#引用式文档一对多关系模型" class="sidebar-link">引用式文档一对多关系模型</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/88c7d3/#树形结构模型" class="sidebar-link">树形结构模型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#具有父节点的树形结构模型" class="sidebar-link">具有父节点的树形结构模型</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#具有子节点的树形结构模型" class="sidebar-link">具有子节点的树形结构模型</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#具有祖先的树形结构模型" class="sidebar-link">具有祖先的树形结构模型</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#具有实体化路径的树形结构模型" class="sidebar-link">具有实体化路径的树形结构模型</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#具有嵌套集的树形结构模型" class="sidebar-link">具有嵌套集的树形结构模型</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/88c7d3/#设计模式" class="sidebar-link">设计模式</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#大文档-很多列-很多索引" class="sidebar-link">大文档,很多列,很多索引</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#管理文档不同版本" class="sidebar-link">管理文档不同版本</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#统计网页点击量" class="sidebar-link">统计网页点击量</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/88c7d3/#精确统计" class="sidebar-link">精确统计</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/88c7d3/#参考资料" class="sidebar-link">参考资料</a></li></ul></li><li><a href="/db-tutorial/pages/10c674/" class="sidebar-link">MongoDB 索引</a></li><li><a href="/db-tutorial/pages/505407/" class="sidebar-link">MongoDB 复制</a></li><li><a href="/db-tutorial/pages/ad08f5/" class="sidebar-link">MongoDB 分片</a></li><li><a href="/db-tutorial/pages/5e3c30/" class="sidebar-link">MongoDB 运维</a></li></ul></section></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>KV数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>列式数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>搜索引擎数据库</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/db-tutorial/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/db-tutorial/categories/?category=%E6%95%B0%E6%8D%AE%E5%BA%93" title="分类" data-v-06225672>数据库</a></li><li data-v-06225672><a href="/db-tutorial/categories/?category=%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93" title="分类" data-v-06225672>文档数据库</a></li><li data-v-06225672><a href="/db-tutorial/categories/?category=MongoDB" title="分类" data-v-06225672>MongoDB</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/dunwu" target="_blank" title="作者" class="beLink" data-v-06225672>dunwu</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2020-09-12</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAAAXNSR0IArs4c6QAABH1JREFUSA3tVl1oHFUUPmdmd2ltklqbpJDiNnXFmgbFktho7YMPNiJSSZM0+CAYSkUELVhM6YuwIPpgoOKDqOBDC0XE2CQoNtQXBUFTTcCi+Wlh1V2TQExsUzcltd3M9Tt3ZjZzZ2fT+OJTL8yeM+eee757fmeJbq//KQL8X3DUSFOcfr7cRsRtxNQMWueeVzOkaITIGqQHNg5y8+jNW9ldM7A6nTpAjuolUikAwq7CE3WcM2RRDz+XGVgN3FptU/aUSlvq9Pa3iZ1+sgAqJyyAFqkipd9dqiwHF3P65YycLWc/6sqGrvoEoIp6DOFaX5h6+dnfjkWprwqsPk0dUGq5vySwDImC10KxFHgGL1SWoc92O3eVht09qdXNH11I2SsTsJYqMWzihqGMi+A+Garf3BAuuLI5oGlULyNfyB/HYNujwktOfRrMr5t77NmevqaUopx0grnKAyvVpmwUDB4x6FPXuGvYLTDwWsejwgtgkYKPqRJg8SV6xaiZ3ZTppGneS4yfH5/66fZSDHv+QZci/+h5c5UHtpy67JUqGppM0sh0Nc1dW6/N1W5Yoqat8/TU/VnadmdeW2PLLSyh0cvxBs3KbqTmwYPpxN4do/mzE8nEpvX/UMu2Wbp74zUAK5q6WkHns7V0eWkdPbPzd3rxkTGybadYySumVzhcaJFbs5UrEkQ/+CK8gF5dnh/6ciIZ73gwQ927L1IitoxKLXYP3SjYdOrHHfTZhRRlFyrorafPk20B3HPD1y2G3qKZME5Jcf3t/HUC13/8tSd++vqFveMUTwAUxSUFI1QekR1+bIze3D9MF2aq6cPvG72CgnldWCFqyRw3lwH8ZMerjTD9ElRO7Gv44wNpC90aASqGfVlz/Rx17srQ57/UU26hkhQqUB7dBR71WmzQhHUnblGmVOEw0jhbV1n9OlXUDCIRGaNV5Jp43N516fN7JmnTHdfp7Hgy0luO4aMhtkLL8Bi3bUWYvzh5Mn1dTxrL6QmGuRhGL/TiTTxRoEdTszSaq9GR0NGA3KdkOz3hqSV3MIDhQ5IVX/Ivx3umBti2es2h4eZby7x8br1rkf7Mo90AqC8aQ3sJeNzqFRu+vSANAQe3PL7l0HGOAdwDCeZYvNKeoZp1Qfs6Aipndh86HmFRi0LAnEO47wsqM6cdfjh3jBPUzhZy7nvlUfFsamED1VQt6aISHVymXZ/B2aCtIG8AI8xfobj2d3en1wWVhOeHELKmLQ1s211s88comkv4UCwWyF787mJdYXtNfhKAXVqnKTq8QZvGAGGOfaTo5pGZ/PwbUCr5+DPr/1J92JNHr9aOl/F3iI5+O1nfybsGxoimvZ3ViWSluDITw3P37mypheDIPY0tw7+O/5ApbkYw+zpfaUVu32Pi98+defdUhEpZkRFq0aqyNh9FuL9hpYbEm6iwi0z2REd09ZmyENEbuhjDWzKvZXTqKYaBIr3tt5kuPtQBZFvEUwHt60vfCNu41XsksH9Ij1BMMz1Y0OOunHNShFIP5868g5zeXmuLwL9T4b6Q2+KejgAAAABJRU5ErkJggg==">MongoDB 建模示例<!----></h1> <div class="theme-vdoing-content content__default"><h1 id="mongodb-建模示例"><a href="#mongodb-建模示例" class="header-anchor">#</a> MongoDB 建模示例</h1> <h2 id="关系型模型"><a href="#关系型模型" class="header-anchor">#</a> 关系型模型</h2> <h3 id="嵌入式文档一对一关系模型"><a href="#嵌入式文档一对一关系模型" class="header-anchor">#</a> 嵌入式文档一对一关系模型</h3> <h4 id="嵌入式文档一对一关系模型-嵌入式文档模式"><a href="#嵌入式文档一对一关系模型-嵌入式文档模式" class="header-anchor">#</a> 嵌入式文档一对一关系模型 - 嵌入式文档模式</h4> <div class="language-json extra-class"><pre class="language-json"><code><span class="token comment">// patron document</span>
<span class="token punctuation">{</span>
_id<span class="token operator">:</span> <span class="token string">&quot;joe&quot;</span><span class="token punctuation">,</span>
name<span class="token operator">:</span> <span class="token string">&quot;Joe Bookreader&quot;</span>
<span class="token punctuation">}</span>
<span class="token comment">// address document</span>
<span class="token punctuation">{</span>
patron_id<span class="token operator">:</span> <span class="token string">&quot;joe&quot;</span><span class="token punctuation">,</span> <span class="token comment">// reference to patron document</span>
street<span class="token operator">:</span> <span class="token string">&quot;123 Fake Street&quot;</span><span class="token punctuation">,</span>
city<span class="token operator">:</span> <span class="token string">&quot;Faketon&quot;</span><span class="token punctuation">,</span>
state<span class="token operator">:</span> <span class="token string">&quot;MA&quot;</span><span class="token punctuation">,</span>
zip<span class="token operator">:</span> <span class="token string">&quot;12345&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>合并为:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;joe&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Joe Bookreader&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;address&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;street&quot;</span><span class="token operator">:</span> <span class="token string">&quot;123 Fake Street&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;city&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Faketon&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;state&quot;</span><span class="token operator">:</span> <span class="token string">&quot;MA&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;zip&quot;</span><span class="token operator">:</span> <span class="token string">&quot;12345&quot;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="嵌入式文档一对一关系模型-子集模式"><a href="#嵌入式文档一对一关系模型-子集模式" class="header-anchor">#</a> 嵌入式文档一对一关系模型 - 子集模式</h4> <p>假设,有一个用于描述电影信息的 collection 定义:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token property">&quot;title&quot;</span><span class="token operator">:</span> <span class="token string">&quot;The Arrival of a Train&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;year&quot;</span><span class="token operator">:</span> <span class="token number">1896</span><span class="token punctuation">,</span>
<span class="token property">&quot;runtime&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token property">&quot;released&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;01-25-1896&quot;</span>)<span class="token punctuation">,</span>
<span class="token property">&quot;poster&quot;</span><span class="token operator">:</span> <span class="token string">&quot;http://ia.media-imdb.com/images/M/MV5BMjEyNDk5MDYzOV5BMl5BanBnXkFtZTgwNjIxMTEwMzE@._V1_SX300.jpg&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;plot&quot;</span><span class="token operator">:</span> <span class="token string">&quot;A group of people are standing in a straight line along the platform of a railway station, waiting for a train, which is seen coming at some distance. When the train stops at the platform, ...&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;fullplot&quot;</span><span class="token operator">:</span> <span class="token string">&quot;A group of people are standing in a straight line along the platform of a railway station, waiting for a train, which is seen coming at some distance. When the train stops at the platform, the line dissolves. The doors of the railway-cars open, and people on the platform help passengers to get off.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;lastupdated&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2015-08-15T10:06:53&quot;</span>)<span class="token punctuation">,</span>
<span class="token property">&quot;type&quot;</span><span class="token operator">:</span> <span class="token string">&quot;movie&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;directors&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Auguste Lumière&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Louis Lumière&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">&quot;imdb&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;rating&quot;</span><span class="token operator">:</span> <span class="token number">7.3</span><span class="token punctuation">,</span>
<span class="token property">&quot;votes&quot;</span><span class="token operator">:</span> <span class="token number">5043</span><span class="token punctuation">,</span>
<span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">12</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token property">&quot;countries&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;France&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">&quot;genres&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Documentary&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Short&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">&quot;tomatoes&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;viewer&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;rating&quot;</span><span class="token operator">:</span> <span class="token number">3.7</span><span class="token punctuation">,</span>
<span class="token property">&quot;numReviews&quot;</span><span class="token operator">:</span> <span class="token number">59</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token property">&quot;lastUpdated&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2020-01-09T00:02:53&quot;</span>)
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>在应用中,有的场景只需要显示电影的简单浏览信息,不需要显示类似 fullplot、poster 这样的详细信息。因为,我们可以考虑将原结构一份为二,并通过 id 字段关联起来。</p> <p>用于展示摘要信息的 movie collection</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token comment">// movie collection</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token property">&quot;title&quot;</span><span class="token operator">:</span> <span class="token string">&quot;The Arrival of a Train&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;year&quot;</span><span class="token operator">:</span> <span class="token number">1896</span><span class="token punctuation">,</span>
<span class="token property">&quot;runtime&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token property">&quot;released&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;1896-01-25&quot;</span>)<span class="token punctuation">,</span>
<span class="token property">&quot;type&quot;</span><span class="token operator">:</span> <span class="token string">&quot;movie&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;directors&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Auguste Lumière&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Louis Lumière&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">&quot;countries&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;France&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">&quot;genres&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Documentary&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Short&quot;</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p>用于展示细节信息的 movie_details collection</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token comment">// movie_details collection</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token number">156</span><span class="token punctuation">,</span>
<span class="token property">&quot;movie_id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token comment">// reference to the movie collection</span>
<span class="token property">&quot;poster&quot;</span><span class="token operator">:</span> <span class="token string">&quot;http://ia.media-imdb.com/images/M/MV5BMjEyNDk5MDYzOV5BMl5BanBnXkFtZTgwNjIxMTEwMzE@._V1_SX300.jpg&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;plot&quot;</span><span class="token operator">:</span> <span class="token string">&quot;A group of people are standing in a straight line along the platform of a railway station, waiting for a train, which is seen coming at some distance. When the train stops at the platform, ...&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;fullplot&quot;</span><span class="token operator">:</span> <span class="token string">&quot;A group of people are standing in a straight line along the platform of a railway station, waiting for a train, which is seen coming at some distance. When the train stops at the platform, the line dissolves. The doors of the railway-cars open, and people on the platform help passengers to get off.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;lastupdated&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2015-08-15T10:06:53&quot;</span>)<span class="token punctuation">,</span>
<span class="token property">&quot;imdb&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;rating&quot;</span><span class="token operator">:</span> <span class="token number">7.3</span><span class="token punctuation">,</span>
<span class="token property">&quot;votes&quot;</span><span class="token operator">:</span> <span class="token number">5043</span><span class="token punctuation">,</span>
<span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">12</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token property">&quot;tomatoes&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;viewer&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">&quot;rating&quot;</span><span class="token operator">:</span> <span class="token number">3.7</span><span class="token punctuation">,</span>
<span class="token property">&quot;numReviews&quot;</span><span class="token operator">:</span> <span class="token number">59</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token property">&quot;lastUpdated&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2020-01-29T00:02:53&quot;</span>)
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="嵌入式文档一对多关系模型"><a href="#嵌入式文档一对多关系模型" class="header-anchor">#</a> 嵌入式文档一对多关系模型</h3> <h4 id="嵌入式文档一对多关系模型-嵌入式文档模式"><a href="#嵌入式文档一对多关系模型-嵌入式文档模式" class="header-anchor">#</a> 嵌入式文档一对多关系模型 - 嵌入式文档模式</h4> <div class="language-json extra-class"><pre class="language-json"><code><span class="token comment">// patron document</span>
<span class="token punctuation">{</span>
_id<span class="token operator">:</span> <span class="token string">&quot;joe&quot;</span><span class="token punctuation">,</span>
name<span class="token operator">:</span> <span class="token string">&quot;Joe Bookreader&quot;</span>
<span class="token punctuation">}</span>
<span class="token comment">// address documents</span>
<span class="token punctuation">{</span>
patron_id<span class="token operator">:</span> <span class="token string">&quot;joe&quot;</span><span class="token punctuation">,</span> <span class="token comment">// reference to patron document</span>
street<span class="token operator">:</span> <span class="token string">&quot;123 Fake Street&quot;</span><span class="token punctuation">,</span>
city<span class="token operator">:</span> <span class="token string">&quot;Faketon&quot;</span><span class="token punctuation">,</span>
state<span class="token operator">:</span> <span class="token string">&quot;MA&quot;</span><span class="token punctuation">,</span>
zip<span class="token operator">:</span> <span class="token string">&quot;12345&quot;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">{</span>
patron_id<span class="token operator">:</span> <span class="token string">&quot;joe&quot;</span><span class="token punctuation">,</span>
street<span class="token operator">:</span> <span class="token string">&quot;1 Some Other Street&quot;</span><span class="token punctuation">,</span>
city<span class="token operator">:</span> <span class="token string">&quot;Boston&quot;</span><span class="token punctuation">,</span>
state<span class="token operator">:</span> <span class="token string">&quot;MA&quot;</span><span class="token punctuation">,</span>
zip<span class="token operator">:</span> <span class="token string">&quot;12345&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>合并为:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;joe&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Joe Bookreader&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;addresses&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;street&quot;</span><span class="token operator">:</span> <span class="token string">&quot;123 Fake Street&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;city&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Faketon&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;state&quot;</span><span class="token operator">:</span> <span class="token string">&quot;MA&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;zip&quot;</span><span class="token operator">:</span> <span class="token string">&quot;12345&quot;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;street&quot;</span><span class="token operator">:</span> <span class="token string">&quot;1 Some Other Street&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;city&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Boston&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;state&quot;</span><span class="token operator">:</span> <span class="token string">&quot;MA&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;zip&quot;</span><span class="token operator">:</span> <span class="token string">&quot;12345&quot;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="嵌入式文档一对多关系模型-子集模式"><a href="#嵌入式文档一对多关系模型-子集模式" class="header-anchor">#</a> 嵌入式文档一对多关系模型 - 子集模式</h4> <p>考虑一个电商网站用于表示商品的 collection</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Super Widget&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;description&quot;</span><span class="token operator">:</span> <span class="token string">&quot;This is the most useful item in your toolbox.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;price&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">&quot;value&quot;</span><span class="token operator">:</span> NumberDecimal(<span class="token string">&quot;119.99&quot;</span>)<span class="token punctuation">,</span> <span class="token property">&quot;currency&quot;</span><span class="token operator">:</span> <span class="token string">&quot;USD&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token property">&quot;reviews&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;review_id&quot;</span><span class="token operator">:</span> <span class="token number">786</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_author&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Kristina&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_text&quot;</span><span class="token operator">:</span> <span class="token string">&quot;This is indeed an amazing widget.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;published_date&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2019-02-18&quot;</span>)
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;review_id&quot;</span><span class="token operator">:</span> <span class="token number">785</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_author&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Trina&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_text&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Nice product. Slow shipping.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;published_date&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2019-02-17&quot;</span>)
<span class="token punctuation">}</span><span class="token punctuation">,</span>
...<span class="token punctuation">{</span>
<span class="token property">&quot;review_id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_author&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Hans&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_text&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Meh, it's okay.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;published_date&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2017-12-06&quot;</span>)
<span class="token punctuation">}</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p>评论按时间倒序排列。 当用户访问产品页面时,应用程序将加载十条最近的评论。可以将集合分为两个集合,而不是与产品一起存储所有评论:</p> <p>产品集合存储有关每个产品的信息,包括产品的十个最新评论:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Super Widget&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;description&quot;</span><span class="token operator">:</span> <span class="token string">&quot;This is the most useful item in your toolbox.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;price&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">&quot;value&quot;</span><span class="token operator">:</span> NumberDecimal(<span class="token string">&quot;119.99&quot;</span>)<span class="token punctuation">,</span> <span class="token property">&quot;currency&quot;</span><span class="token operator">:</span> <span class="token string">&quot;USD&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token property">&quot;reviews&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;review_id&quot;</span><span class="token operator">:</span> <span class="token number">786</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_author&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Kristina&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_text&quot;</span><span class="token operator">:</span> <span class="token string">&quot;This is indeed an amazing widget.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;published_date&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2019-02-18&quot;</span>)
<span class="token punctuation">}</span>
...
<span class="token punctuation">{</span>
<span class="token property">&quot;review_id&quot;</span><span class="token operator">:</span> <span class="token number">776</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_author&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Pablo&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_text&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Amazing!&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;published_date&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2019-02-16&quot;</span>)
<span class="token punctuation">}</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre></div><p>review collection 存储所有的评论</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
<span class="token property">&quot;review_id&quot;</span><span class="token operator">:</span> <span class="token number">786</span><span class="token punctuation">,</span>
<span class="token property">&quot;product_id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_author&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Kristina&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_text&quot;</span><span class="token operator">:</span> <span class="token string">&quot;This is indeed an amazing widget.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;published_date&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2019-02-18&quot;</span>)
<span class="token punctuation">}</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;review_id&quot;</span><span class="token operator">:</span> <span class="token number">785</span><span class="token punctuation">,</span>
<span class="token property">&quot;product_id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_author&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Trina&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_text&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Nice product. Slow shipping.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;published_date&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2019-02-17&quot;</span>)
<span class="token punctuation">}</span>
...
<span class="token punctuation">{</span>
<span class="token property">&quot;review_id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token property">&quot;product_id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_author&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Hans&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;review_text&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Meh, it's okay.&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;published_date&quot;</span><span class="token operator">:</span> ISODate(<span class="token string">&quot;2017-12-06&quot;</span>)
<span class="token punctuation">}</span>
</code></pre></div><h3 id="引用式文档一对多关系模型"><a href="#引用式文档一对多关系模型" class="header-anchor">#</a> 引用式文档一对多关系模型</h3> <p>考虑以下映射出版商和书籍关系的示例。</p> <p>该示例说明了引用式文档的优点,以避免重复发布者信息。</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
title<span class="token operator">:</span> <span class="token string">&quot;MongoDB: The Definitive Guide&quot;</span><span class="token punctuation">,</span>
author<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token string">&quot;Kristina Chodorow&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Mike Dirolf&quot;</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
published_date<span class="token operator">:</span> ISODate(<span class="token string">&quot;2010-09-24&quot;</span>)<span class="token punctuation">,</span>
pages<span class="token operator">:</span> <span class="token number">216</span><span class="token punctuation">,</span>
language<span class="token operator">:</span> <span class="token string">&quot;English&quot;</span><span class="token punctuation">,</span>
publisher<span class="token operator">:</span> <span class="token punctuation">{</span>
name<span class="token operator">:</span> <span class="token string">&quot;O'Reilly Media&quot;</span><span class="token punctuation">,</span>
founded<span class="token operator">:</span> <span class="token number">1980</span><span class="token punctuation">,</span>
location<span class="token operator">:</span> <span class="token string">&quot;CA&quot;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">{</span>
title<span class="token operator">:</span> <span class="token string">&quot;50 Tips and Tricks for MongoDB Developer&quot;</span><span class="token punctuation">,</span>
author<span class="token operator">:</span> <span class="token string">&quot;Kristina Chodorow&quot;</span><span class="token punctuation">,</span>
published_date<span class="token operator">:</span> ISODate(<span class="token string">&quot;2011-05-06&quot;</span>)<span class="token punctuation">,</span>
pages<span class="token operator">:</span> <span class="token number">68</span><span class="token punctuation">,</span>
language<span class="token operator">:</span> <span class="token string">&quot;English&quot;</span><span class="token punctuation">,</span>
publisher<span class="token operator">:</span> <span class="token punctuation">{</span>
name<span class="token operator">:</span> <span class="token string">&quot;O'Reilly Media&quot;</span><span class="token punctuation">,</span>
founded<span class="token operator">:</span> <span class="token number">1980</span><span class="token punctuation">,</span>
location<span class="token operator">:</span> <span class="token string">&quot;CA&quot;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>为避免重复出版商数据,可以使用引用型文档,并将出版商信息与书本分开保存。 使用引用时,关系的增长决定了将引用存储在何处。 如果每个出版商的图书数量很少且增长有限,则有时将图书参考存储在出版商文档中可能会很有用。 否则,如果每个发布者的书籍数量不受限制,则此数据模型将导致可变的,不断增长的数组,如以下示例所示:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
name<span class="token operator">:</span> <span class="token string">&quot;O'Reilly Media&quot;</span><span class="token punctuation">,</span>
founded<span class="token operator">:</span> <span class="token number">1980</span><span class="token punctuation">,</span>
location<span class="token operator">:</span> <span class="token string">&quot;CA&quot;</span><span class="token punctuation">,</span>
books<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">123456789</span><span class="token punctuation">,</span> <span class="token number">234567890</span><span class="token punctuation">,</span> ...<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span class="token punctuation">{</span>
_id<span class="token operator">:</span> <span class="token number">123456789</span><span class="token punctuation">,</span>
title<span class="token operator">:</span> <span class="token string">&quot;MongoDB: The Definitive Guide&quot;</span><span class="token punctuation">,</span>
author<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token string">&quot;Kristina Chodorow&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Mike Dirolf&quot;</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
published_date<span class="token operator">:</span> ISODate(<span class="token string">&quot;2010-09-24&quot;</span>)<span class="token punctuation">,</span>
pages<span class="token operator">:</span> <span class="token number">216</span><span class="token punctuation">,</span>
language<span class="token operator">:</span> <span class="token string">&quot;English&quot;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">{</span>
_id<span class="token operator">:</span> <span class="token number">234567890</span><span class="token punctuation">,</span>
title<span class="token operator">:</span> <span class="token string">&quot;50 Tips and Tricks for MongoDB Developer&quot;</span><span class="token punctuation">,</span>
author<span class="token operator">:</span> <span class="token string">&quot;Kristina Chodorow&quot;</span><span class="token punctuation">,</span>
published_date<span class="token operator">:</span> ISODate(<span class="token string">&quot;2011-05-06&quot;</span>)<span class="token punctuation">,</span>
pages<span class="token operator">:</span> <span class="token number">68</span><span class="token punctuation">,</span>
language<span class="token operator">:</span> <span class="token string">&quot;English&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>为了避免可变的,增长的数组,请将发行者参考存储在书籍文档中:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span>
_id<span class="token operator">:</span> <span class="token string">&quot;oreilly&quot;</span><span class="token punctuation">,</span>
name<span class="token operator">:</span> <span class="token string">&quot;O'Reilly Media&quot;</span><span class="token punctuation">,</span>
founded<span class="token operator">:</span> <span class="token number">1980</span><span class="token punctuation">,</span>
location<span class="token operator">:</span> <span class="token string">&quot;CA&quot;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">{</span>
_id<span class="token operator">:</span> <span class="token number">123456789</span><span class="token punctuation">,</span>
title<span class="token operator">:</span> <span class="token string">&quot;MongoDB: The Definitive Guide&quot;</span><span class="token punctuation">,</span>
author<span class="token operator">:</span> <span class="token punctuation">[</span> <span class="token string">&quot;Kristina Chodorow&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Mike Dirolf&quot;</span> <span class="token punctuation">]</span><span class="token punctuation">,</span>
published_date<span class="token operator">:</span> ISODate(<span class="token string">&quot;2010-09-24&quot;</span>)<span class="token punctuation">,</span>
pages<span class="token operator">:</span> <span class="token number">216</span><span class="token punctuation">,</span>
language<span class="token operator">:</span> <span class="token string">&quot;English&quot;</span><span class="token punctuation">,</span>
publisher_id<span class="token operator">:</span> <span class="token string">&quot;oreilly&quot;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">{</span>
_id<span class="token operator">:</span> <span class="token number">234567890</span><span class="token punctuation">,</span>
title<span class="token operator">:</span> <span class="token string">&quot;50 Tips and Tricks for MongoDB Developer&quot;</span><span class="token punctuation">,</span>
author<span class="token operator">:</span> <span class="token string">&quot;Kristina Chodorow&quot;</span><span class="token punctuation">,</span>
published_date<span class="token operator">:</span> ISODate(<span class="token string">&quot;2011-05-06&quot;</span>)<span class="token punctuation">,</span>
pages<span class="token operator">:</span> <span class="token number">68</span><span class="token punctuation">,</span>
language<span class="token operator">:</span> <span class="token string">&quot;English&quot;</span><span class="token punctuation">,</span>
publisher_id<span class="token operator">:</span> <span class="token string">&quot;oreilly&quot;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="树形结构模型"><a href="#树形结构模型" class="header-anchor">#</a> 树形结构模型</h2> <p><img src="https://raw.githubusercontent.com/dunwu/images/master/snap/20200911194846.svg" alt="img"></p> <h3 id="具有父节点的树形结构模型"><a href="#具有父节点的树形结构模型" class="header-anchor">#</a> 具有父节点的树形结构模型</h3> <p>上图结构可以用父引用来表示:</p> <div class="language-json extra-class"><pre class="language-json"><code>db.categories.insertMany(<span class="token punctuation">[</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;MongoDB&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Databases&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;dbm&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Databases&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Databases&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Programming&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Languages&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Programming&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Programming&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Books&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Books&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token null keyword">null</span> <span class="token punctuation">}</span>
<span class="token punctuation">]</span>)
</code></pre></div><ul><li><p>检索节点的父节点:</p> <div class="language- extra-class"><pre class="language-text"><code>db.categories.findOne( { _id: &quot;MongoDB&quot; } ).parent
</code></pre></div></li> <li><p>可以在父字段上创建索引以启用父节点的快速搜索:</p> <div class="language- extra-class"><pre class="language-text"><code>db.categories.createIndex( { parent: 1 } )
</code></pre></div></li> <li><p>可以通过父字段查询找到其直接子节点:</p> <div class="language- extra-class"><pre class="language-text"><code>db.categories.find( { parent: &quot;Databases&quot; } )
</code></pre></div></li> <li><p>检索子树,可以参考: <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/#pipe._S_graphLookup" target="_blank" rel="noopener noreferrer"><code>$graphLookup</code><span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>.</p></li></ul> <h3 id="具有子节点的树形结构模型"><a href="#具有子节点的树形结构模型" class="header-anchor">#</a> 具有子节点的树形结构模型</h3> <div class="language-json extra-class"><pre class="language-json"><code>db.categories.insertMany(<span class="token punctuation">[</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;MongoDB&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;children&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;dbm&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;children&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Databases&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;children&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;MongoDB&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;dbm&quot;</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Languages&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;children&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Programming&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;children&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Databases&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Languages&quot;</span><span class="token punctuation">]</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Books&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;children&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Programming&quot;</span><span class="token punctuation">]</span> <span class="token punctuation">}</span>
<span class="token punctuation">]</span>)
</code></pre></div><ul><li><p>检索节点的 children</p> <div class="language- extra-class"><pre class="language-text"><code>db.categories.findOne( { _id: &quot;Databases&quot; } ).children
</code></pre></div></li> <li><p>可以在 children 字段上创建索引以启用子节点的快速搜索:</p> <div class="language- extra-class"><pre class="language-text"><code>db.categories.createIndex( { children: 1 } )
</code></pre></div></li> <li><p>可以在 children 字段中查询节点,以找到其父节点及其兄弟节点:</p> <div class="language- extra-class"><pre class="language-text"><code>db.categories.find( { children: &quot;MongoDB&quot; } )
</code></pre></div></li></ul> <h3 id="具有祖先的树形结构模型"><a href="#具有祖先的树形结构模型" class="header-anchor">#</a> 具有祖先的树形结构模型</h3> <div class="language-json extra-class"><pre class="language-json"><code>db.categories.insertMany(<span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;MongoDB&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;ancestors&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Books&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Programming&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Databases&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Databases&quot;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;dbm&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;ancestors&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Books&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Programming&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Databases&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Databases&quot;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Databases&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;ancestors&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Books&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Programming&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Programming&quot;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span>
<span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Languages&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;ancestors&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Books&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;Programming&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Programming&quot;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Programming&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;ancestors&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">&quot;Books&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Books&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Books&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;ancestors&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token property">&quot;parent&quot;</span><span class="token operator">:</span> <span class="token null keyword">null</span> <span class="token punctuation">}</span>
<span class="token punctuation">]</span>)
</code></pre></div><ul><li><p>检索节点的祖先或路径的查询是快速而直接的:</p> <div class="language-json extra-class"><pre class="language-json"><code>db.categories.findOne(<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;MongoDB&quot;</span> <span class="token punctuation">}</span>).ancestors
</code></pre></div></li> <li><p>可以在 ancestors 字段上创建索引,以启用祖先节点的快速搜索:</p> <div class="language-json extra-class"><pre class="language-json"><code>db.categories.createIndex(<span class="token punctuation">{</span> <span class="token property">&quot;ancestors&quot;</span><span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span>)
</code></pre></div></li> <li><p>可以通过 ancestors 字段查询查找其所有后代:</p> <div class="language-json extra-class"><pre class="language-json"><code>db.categories.find(<span class="token punctuation">{</span> <span class="token property">&quot;ancestors&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Programming&quot;</span> <span class="token punctuation">}</span>)
</code></pre></div></li></ul> <h3 id="具有实体化路径的树形结构模型"><a href="#具有实体化路径的树形结构模型" class="header-anchor">#</a> 具有实体化路径的树形结构模型</h3> <div class="language-json extra-class"><pre class="language-json"><code>db.categories.insertMany(<span class="token punctuation">[</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Books&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;path&quot;</span><span class="token operator">:</span> <span class="token null keyword">null</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Programming&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;path&quot;</span><span class="token operator">:</span> <span class="token string">&quot;,Books,&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Databases&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;path&quot;</span><span class="token operator">:</span> <span class="token string">&quot;,Books,Programming,&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Languages&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;path&quot;</span><span class="token operator">:</span> <span class="token string">&quot;,Books,Programming,&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;MongoDB&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;path&quot;</span><span class="token operator">:</span> <span class="token string">&quot;,Books,Programming,Databases,&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token property">&quot;_id&quot;</span><span class="token operator">:</span> <span class="token string">&quot;dbm&quot;</span><span class="token punctuation">,</span> <span class="token property">&quot;path&quot;</span><span class="token operator">:</span> <span class="token string">&quot;,Books,Programming,Databases,&quot;</span> <span class="token punctuation">}</span>
<span class="token punctuation">]</span>)
</code></pre></div><ul><li><p>可以查询以检索整个树,并按字段路径排序:</p> <div class="language- extra-class"><pre class="language-text"><code>db.categories.find().sort( { path: 1 } )
</code></pre></div></li> <li><p>可以在 path 字段上使用正则表达式来查找 Programming 的后代</p> <div class="language- extra-class"><pre class="language-text"><code>db.categories.find( { path: /,Programming,/ } )
</code></pre></div></li> <li><p>可以检索 Books 的后代,其中 Books 也位于层次结构的最高级别:</p> <div class="language- extra-class"><pre class="language-text"><code>db.categories.find( { path: /^,Books,/ } )
</code></pre></div></li> <li><p>要在 path 字段上创建索引,请使用以下调用:</p> <div class="language- extra-class"><pre class="language-text"><code>db.categories.createIndex( { path: 1 } )
</code></pre></div></li></ul> <h3 id="具有嵌套集的树形结构模型"><a href="#具有嵌套集的树形结构模型" class="header-anchor">#</a> 具有嵌套集的树形结构模型</h3> <p><img src="https://raw.githubusercontent.com/dunwu/images/master/snap/20200911204252.svg" alt="img"></p> <div class="language-javascript extra-class"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>categories<span class="token punctuation">.</span><span class="token function">insertMany</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
<span class="token punctuation">{</span> <span class="token literal-property property">_id</span><span class="token operator">:</span> <span class="token string">'Books'</span><span class="token punctuation">,</span> <span class="token literal-property property">parent</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token literal-property property">left</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">right</span><span class="token operator">:</span> <span class="token number">12</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token literal-property property">_id</span><span class="token operator">:</span> <span class="token string">'Programming'</span><span class="token punctuation">,</span> <span class="token literal-property property">parent</span><span class="token operator">:</span> <span class="token string">'Books'</span><span class="token punctuation">,</span> <span class="token literal-property property">left</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token literal-property property">right</span><span class="token operator">:</span> <span class="token number">11</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token literal-property property">_id</span><span class="token operator">:</span> <span class="token string">'Languages'</span><span class="token punctuation">,</span> <span class="token literal-property property">parent</span><span class="token operator">:</span> <span class="token string">'Programming'</span><span class="token punctuation">,</span> <span class="token literal-property property">left</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token literal-property property">right</span><span class="token operator">:</span> <span class="token number">4</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token literal-property property">_id</span><span class="token operator">:</span> <span class="token string">'Databases'</span><span class="token punctuation">,</span> <span class="token literal-property property">parent</span><span class="token operator">:</span> <span class="token string">'Programming'</span><span class="token punctuation">,</span> <span class="token literal-property property">left</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token literal-property property">right</span><span class="token operator">:</span> <span class="token number">10</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token literal-property property">_id</span><span class="token operator">:</span> <span class="token string">'MongoDB'</span><span class="token punctuation">,</span> <span class="token literal-property property">parent</span><span class="token operator">:</span> <span class="token string">'Databases'</span><span class="token punctuation">,</span> <span class="token literal-property property">left</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token literal-property property">right</span><span class="token operator">:</span> <span class="token number">7</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span> <span class="token literal-property property">_id</span><span class="token operator">:</span> <span class="token string">'dbm'</span><span class="token punctuation">,</span> <span class="token literal-property property">parent</span><span class="token operator">:</span> <span class="token string">'Databases'</span><span class="token punctuation">,</span> <span class="token literal-property property">left</span><span class="token operator">:</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token literal-property property">right</span><span class="token operator">:</span> <span class="token number">9</span> <span class="token punctuation">}</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre></div><p>可以查询以检索节点的后代:</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> databaseCategory <span class="token operator">=</span> db<span class="token punctuation">.</span>categories<span class="token punctuation">.</span><span class="token function">findOne</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">_id</span><span class="token operator">:</span> <span class="token string">'Databases'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
db<span class="token punctuation">.</span>categories<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
<span class="token literal-property property">left</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">$gt</span><span class="token operator">:</span> databaseCategory<span class="token punctuation">.</span>left <span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token literal-property property">right</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">$lt</span><span class="token operator">:</span> databaseCategory<span class="token punctuation">.</span>right <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="设计模式"><a href="#设计模式" class="header-anchor">#</a> 设计模式</h2> <h3 id="大文档-很多列-很多索引"><a href="#大文档-很多列-很多索引" class="header-anchor">#</a> 大文档,很多列,很多索引</h3> <p>解决方案是:列转行</p> <p><img src="https://raw.githubusercontent.com/dunwu/images/master/snap/20200919225901.png" alt="img"></p> <h3 id="管理文档不同版本"><a href="#管理文档不同版本" class="header-anchor">#</a> 管理文档不同版本</h3> <p>MongoDB 文档格式非常灵活,势必会带来版本维护上的难度。</p> <p>解决方案是:可以增加一个版本号字段</p> <ul><li>快速过滤掉不需要升级的文档</li> <li>升级时,对不同版本的文档做不同处理</li></ul> <h3 id="统计网页点击量"><a href="#统计网页点击量" class="header-anchor">#</a> 统计网页点击量</h3> <p>统计数据精确性要求并不是十分重要。</p> <p>解决方案:用近似计算</p> <p>每隔 10 次写一次:</p> <div class="language-json extra-class"><pre class="language-json"><code><span class="token punctuation">{</span> <span class="token property">&quot;$inc&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">&quot;views&quot;</span><span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
</code></pre></div><h3 id="精确统计"><a href="#精确统计" class="header-anchor">#</a> 精确统计</h3> <p>解决方案:使用预聚合</p> <h2 id="参考资料"><a href="#参考资料" class="header-anchor">#</a> 参考资料</h2> <ul><li><a href="https://docs.mongodb.com/manual/applications/data-models/" target="_blank" rel="noopener noreferrer">Data Model Examples and Patterns<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/dunwu/db-tutorial/edit/master/docs/12.数据库/04.文档数据库/01.MongoDB/06.MongoDB建模示例.md" target="_blank" rel="noopener noreferrer">📝 帮助改善此页面!</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/db-tutorial/tags/?tag=%E6%95%B0%E6%8D%AE%E5%BA%93" title="标签">#数据库</a><a href="/db-tutorial/tags/?tag=%E6%96%87%E6%A1%A3%E6%95%B0%E6%8D%AE%E5%BA%93" title="标签">#文档数据库</a><a href="/db-tutorial/tags/?tag=MongoDB" title="标签">#MongoDB</a><a href="/db-tutorial/tags/?tag=%E5%BB%BA%E6%A8%A1" title="标签">#建模</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2024/10/09, 07:16:02</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/db-tutorial/pages/562f99/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">MongoDB 建模</div></a> <a href="/db-tutorial/pages/10c674/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">MongoDB 索引</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
<a href="/db-tutorial/pages/562f99/" class="prev">MongoDB 建模</a></span> <span class="next"><a href="/db-tutorial/pages/10c674/">MongoDB 索引</a>
</span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/db-tutorial/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/db-tutorial/pages/b59ba2/"><div>
HBase Java API 管理功能
<!----></div></a> <span class="date">04-13</span></dt></dl><dl><dd>02</dd> <dt><a href="/db-tutorial/pages/ce5ca0/"><div>
HBase Java API 其他高级特性
<!----></div></a> <span class="date">03-31</span></dt></dl><dl><dd>03</dd> <dt><a href="/db-tutorial/pages/c8cfeb/"><div>
HBase 数据模型
<!----></div></a> <span class="date">03-16</span></dt></dl> <dl><dd></dd> <dt><a href="/db-tutorial/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:forbreak@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/dunwu" title="GitHub" target="_blank" class="iconfont icon-github"></a></div>
Theme by
<a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a>
| Copyright © 2019-2024
<span>钝悟dunwu | CC-BY-SA-4.0</span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
跟随系统
</li><li class="iconfont icon-rijianmoshi">
浅色模式
</li><li class="iconfont icon-yejianmoshi">
深色模式
</li><li class="iconfont icon-yuedu">
阅读模式
</li></ul></div></div> <!----> <!----> <div class="custom-html-window custom-html-window-rb" style="display:;"><div class="custom-wrapper"><span class="close-but">×</span> <div>
<div class="wwads-cn wwads-vertical windowRB" data-id="261" style="max-width:160px;
min-width: auto;min-height:auto;"></div>
<style>
.windowRB{ padding: 0;}
.windowRB .wwads-img{margin-top: 10px;}
.windowRB .wwads-content{margin: 0 10px 40px 10px;}
.custom-html-window-rb .close-but{
display: none;
}
</style>
</div></div></div></div><div class="global-ui"><div></div></div></div>
<script src="/db-tutorial/assets/js/app.be3f2e92.js" defer></script><script src="/db-tutorial/assets/js/2.aded268b.js" defer></script><script src="/db-tutorial/assets/js/38.5024023f.js" defer></script>
</body>
</html>