mirror of https://github.com/dunwu/db-tutorial.git
196 lines
98 KiB
HTML
196 lines
98 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html lang="en-US">
|
|||
|
<head>
|
|||
|
<meta charset="utf-8">
|
|||
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|||
|
<title>MongoDB 的 CRUD 操作 | 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/34.1ba1a06c.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/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/38.5024023f.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="prefetc
|
|||
|
<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/" aria-current="page" class="active sidebar-link">MongoDB 的 CRUD 操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/7efbac/#一、基本-crud-操作" class="sidebar-link">一、基本 CRUD 操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/7efbac/#create-操作" class="sidebar-link">Create 操作</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/7efbac/#read-操作" class="sidebar-link">Read 操作</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/7efbac/#update-操作" class="sidebar-link">Update 操作</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/7efbac/#delete-操作" class="sidebar-link">Delete 操作</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/7efbac/#二、批量写操作" class="sidebar-link">二、批量写操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/7efbac/#有序和无序的操作" class="sidebar-link">有序和无序的操作</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/7efbac/#bulkwrite-方法" class="sidebar-link">bulkWrite() 方法</a></li><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/7efbac/#批量写操作策略" class="sidebar-link">批量写操作策略</a></li><li class="sidebar-sub-header level4"><a href="/db-tutorial/pages/7efbac/#预拆分-collection" class="sidebar-link">预拆分 collection</a></li><li class="sidebar-sub-header level4"><a href="/db-tutorial/pages/7efbac/#无序写操作" class="sidebar-link">无序写操作</a></li><li class="sidebar-sub-header level4"><a href="/db-tutorial/pages/7efbac/#避免单调节流" class="sidebar-link">避免单调节流</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/7efbac/#sql-和-mongodb-对比" class="sidebar-link">SQL 和 MongoDB 对比</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/db-tutorial/pages/7efbac/#术语和概念" class="sidebar-link">术语和概念</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/db-tutorial/pages/7efbac/#参考资料" class="sidebar-link">参考资料</a></li></ul></li><li><a href="/db-tutorial/pages/75daa5/" class="sidebar-link">MongoDB 的聚合操作</a></li><li><a href="/db-tutorial/pages/4574fe/" class="s
|
|||
|
<span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'canvas'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">tags</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'cotton'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">35.5</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">}</span><span class="token punctuation">)</span>
|
|||
|
</code></pre></div><p>【示例】插入多条 document 示例</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>inventory<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">item</span><span class="token operator">:</span> <span class="token string">'journal'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">25</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">tags</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'blank'</span><span class="token punctuation">,</span> <span class="token string">'red'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">21</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'mat'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">85</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">tags</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'gray'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">27.9</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">35.5</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'mousepad'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">25</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">tags</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">'gel'</span><span class="token punctuation">,</span> <span class="token string">'blue'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">19</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">22.85</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">]</span><span class="token punctuation">)</span>
|
|||
|
</code></pre></div><h3 id="read-操作"><a href="#read-操作" class="header-anchor">#</a> Read 操作</h3> <p>MongoDB 提供 <a href="https://docs.mongodb.com/manual/reference/method/db.collection.find/#db.collection.find" target="_blank" rel="noopener noreferrer"><code>db.collection.find()</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> 方法来检索 document。</p> <p><img src="https://raw.githubusercontent.com/dunwu/images/master/snap/20200924113832.svg" alt="img"></p> <h3 id="update-操作"><a href="#update-操作" class="header-anchor">#</a> Update 操作</h3> <p>MongoDB 提供以下操作来更新 collection 中的 document</p> <ul><li><a href="https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#db.collection.updateOne" target="_blank" rel="noopener noreferrer"><code>db.collection.updateOne()</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>:更新一条 document</li> <li><a href="https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/#db.collection.updateMany" target="_blank" rel="noopener noreferrer"><code>db.collection.updateMany()</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>:更新多条 document</li> <li><a href="https://docs.mongodb.com/manual/reference/method/db.collection.replaceOne/#db.collection.replaceOne" target="_blank" rel="noopener noreferrer"><code>db.collection.replaceOne()</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>:替换一条 document</li></ul> <p>语法格式:</p> <ul><li><a href="https://docs.mongodb.com/manual/reference/method/db.collection.updateOne/#db.collection.updateOne" target="_blank" rel="noopener noreferrer"><code>db.collection.updateOne(<filter>, <update>, <options>)</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"
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'canvas'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">35.5</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'A'</span>
|
|||
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span> <span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'journal'</span><span class="token punctuation">,</span> <span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">25</span><span class="token punctuation">,</span> <span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">21</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'A'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span> <span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'mat'</span><span class="token punctuation">,</span> <span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">85</span><span class="token punctuation">,</span> <span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">27.9</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">35.5</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'A'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'mousepad'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">25</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">19</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">22.85</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'P'</span>
|
|||
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'notebook'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">50</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">8.5</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'in'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'P'</span>
|
|||
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span> <span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'paper'</span><span class="token punctuation">,</span> <span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">8.5</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">11</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'in'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'D'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'planner'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">75</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">22.85</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'D'</span>
|
|||
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'postcard'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">45</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">15.25</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'A'</span>
|
|||
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'sketchbook'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">80</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">14</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">21</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'A'</span>
|
|||
|
<span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'sketch pad'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">95</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">size</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">h</span><span class="token operator">:</span> <span class="token number">22.85</span><span class="token punctuation">,</span> <span class="token literal-property property">w</span><span class="token operator">:</span> <span class="token number">30.5</span><span class="token punctuation">,</span> <span class="token literal-property property">uom</span><span class="token operator">:</span> <span class="token string">'cm'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'A'</span>
|
|||
|
<span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">]</span><span class="token punctuation">)</span>
|
|||
|
</code></pre></div><p>【示例】更新一条 document</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>inventory<span class="token punctuation">.</span><span class="token function">updateOne</span><span class="token punctuation">(</span>
|
|||
|
<span class="token punctuation">{</span> <span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'paper'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">$set</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token string-property property">'size.uom'</span><span class="token operator">:</span> <span class="token string">'cm'</span><span class="token punctuation">,</span> <span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'P'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">$currentDate</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">lastModified</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">)</span>
|
|||
|
</code></pre></div><p>【示例】更新多条 document</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>inventory<span class="token punctuation">.</span><span class="token function">updateMany</span><span class="token punctuation">(</span>
|
|||
|
<span class="token punctuation">{</span> <span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">$lt</span><span class="token operator">:</span> <span class="token number">50</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">$set</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token string-property property">'size.uom'</span><span class="token operator">:</span> <span class="token string">'in'</span><span class="token punctuation">,</span> <span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'P'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">$currentDate</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">lastModified</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">)</span>
|
|||
|
</code></pre></div><p>【示例】替换一条 document</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code>db<span class="token punctuation">.</span>inventory<span class="token punctuation">.</span><span class="token function">replaceOne</span><span class="token punctuation">(</span>
|
|||
|
<span class="token punctuation">{</span> <span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'paper'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">item</span><span class="token operator">:</span> <span class="token string">'paper'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">instock</span><span class="token operator">:</span> <span class="token punctuation">[</span>
|
|||
|
<span class="token punctuation">{</span> <span class="token literal-property property">warehouse</span><span class="token operator">:</span> <span class="token string">'A'</span><span class="token punctuation">,</span> <span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">60</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span> <span class="token literal-property property">warehouse</span><span class="token operator">:</span> <span class="token string">'B'</span><span class="token punctuation">,</span> <span class="token literal-property property">qty</span><span class="token operator">:</span> <span class="token number">40</span> <span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">]</span>
|
|||
|
<span class="token punctuation">}</span>
|
|||
|
<span class="token punctuation">)</span>
|
|||
|
</code></pre></div><p>更新操作的特性:</p> <ul><li>MongoDB 中的所有写操作都是单个文档级别的原子操作。</li> <li>一旦设置了,就无法更新或替换 <a href="https://docs.mongodb.com/manual/reference/glossary/#term-id" target="_blank" rel="noopener noreferrer"><code>_id</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> 字段。</li> <li>除以下情况外,MongoDB 会在执行写操作后保留文档字段的顺序:
|
|||
|
<ul><li><code>_id</code> 字段始终是文档中的第一个字段。</li> <li>包括重命名字段名称的更新可能导致文档中字段的重新排序。</li></ul></li> <li>如果更新操作中包含 <code>upsert : true</code> 并且没有 document 匹配过滤器,MongoDB 会新插入一个 document;如果有匹配的 document,MongoDB 会修改或替换这些 document。</li></ul> <h3 id="delete-操作"><a href="#delete-操作" class="header-anchor">#</a> Delete 操作</h3> <p>MongoDB 提供以下操作来删除 collection 中的 document</p> <ul><li><a href="https://docs.mongodb.com/manual/reference/method/db.collection.deleteOne/#db.collection.deleteOne" target="_blank" rel="noopener noreferrer"><code>db.collection.deleteOne()</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>:删除一条 document</li> <li><a href="https://docs.mongodb.com/manual/reference/method/db.collection.deleteMany/#db.collection.deleteMany" target="_blank" rel="noopener noreferrer"><code>db.collection.deleteMany()</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>:删除多条 document</li></ul> <p><img src="https://raw.githubusercontent.com/dunwu/images/master/snap/20200924120007.svg" alt="img"></p> <p>删除操作的特性:</p> <ul><li>MongoDB 中的所有写操作都是单个文档级别的原子操作。</li></ul> <h2 id="二、批量写操作"><a href="#二、批量写操作" class="header-anchor">#</a> 二、批量写操作</h2> <p>MongoDB 通过 <a href="https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/#db.collection.bulkWrite" target="_blank" rel="noopener noreferrer"><code>db.collection.bulkWrite()</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> <p>此外,<a href="https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/#db.collection.insertMany" target="_blank" rel="noopener noreferrer"><code>db.collection.insertMany()</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> <h3 id="有序和无序的操作"><a href="#有序和无序的操作" class="header-anchor">#</a> 有<>
|
|||
|
db<span class="token punctuation">.</span>characters<span class="token punctuation">.</span><span class="token function">bulkWrite</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">insertOne</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">document</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">_id</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">char</span><span class="token operator">:</span> <span class="token string">'Dithras'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token keyword">class</span><span class="token operator">:</span> <span class="token string">'barbarian'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">lvl</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 punctuation">,</span>
|
|||
|
<span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">insertOne</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">document</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">_id</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">char</span><span class="token operator">:</span> <span class="token string">'Taeln'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token keyword">class</span><span class="token operator">:</span> <span class="token string">'fighter'</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">lvl</span><span class="token operator">:</span> <span class="token number">3</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 literal-property property">updateOne</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">filter</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">char</span><span class="token operator">:</span> <span class="token string">'Eldon'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">update</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">$set</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">status</span><span class="token operator">:</span> <span class="token string">'Critical Injury'</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 punctuation">{</span> <span class="token literal-property property">deleteOne</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">filter</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">char</span><span class="token operator">:</span> <span class="token string">'Brisbane'</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 literal-property property">replaceOne</span><span class="token operator">:</span> <span class="token punctuation">{</span>
|
|||
|
<span class="token literal-property property">filter</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">char</span><span class="token operator">:</span> <span class="token string">'Meldane'</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
|
|||
|
<span class="token literal-property property">replacement</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">char</span><span class="token operator">:</span> <span class="token string">'Tanys'</span><span class="token punctuation">,</span> <span class="token keyword">class</span><span class="token operator">:</span> <span class="token string">'oracle'</span><span class="token punctuation">,</span> <span class="token literal-property property">lvl</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 punctuation">]</span><span class="token punctuation">)</span>
|
|||
|
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
|
|||
|
<span class="token function">print</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span>
|
|||
|
<span class="token punctuation">}</span>
|
|||
|
</code></pre></div><h3 id="批量写操作策略"><a href="#批量写操作策略" class="header-anchor">#</a> 批量写操作策略</h3> <p>大量的插入操作(包括初始数据插入或常规数据导入)可能会影响分片集群的性能。对于批量插入,请考虑以下策略:</p> <h4 id="预拆分-collection"><a href="#预拆分-collection" class="header-anchor">#</a> 预拆分 collection</h4> <p>如果分片集合为空,则该集合只有一个初始 <a href="https://docs.mongodb.com/manual/reference/glossary/#term-chunk" target="_blank" rel="noopener noreferrer">chunk<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>,该 <a href="https://docs.mongodb.com/manual/reference/glossary/#term-chunk" target="_blank" rel="noopener noreferrer">chunk<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> 位于单个分片上。然后,MongoDB 必须花一些时间来接收数据,创建拆分并将拆分的块分发到可用的分片。为了避免这种性能成本,您可以按照拆分群集中的拆分块中的说明预拆分 collection。</p> <h4 id="无序写操作"><a href="#无序写操作" class="header-anchor">#</a> 无序写操作</h4> <p>要提高对分片集群的写入性能,请使用 <a href="https://docs.mongodb.com/manual/reference/method/db.collection.bulkWrite/#db.collection.bulkWrite" target="_blank" rel="noopener noreferrer"><code>bulkWrite()</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>,并将可选参数顺序设置为 false。<a href="https://docs.mongodb.com/manual/reference/program/mongos/#bin.mongos" target="_blank" rel="noopener noreferrer"><code>mongos</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> 可以尝试同时将写入操作发送到多个分片。对于空集合,首先按照分片群集中的分割 <a href="https://docs.mongodb.com/manual/reference/glossary/#term-chunk" target="_blank" rel="noopener noreferrer">chunk<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.
|
|||
|
←
|
|||
|
<a href="/db-tutorial/pages/3288f3/" class="prev">MongoDB 应用指南</a></span> <span class="next"><a href="/db-tutorial/pages/75daa5/">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">更多文章></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/34.1ba1a06c.js" defer></script>
|
|||
|
</body>
|
|||
|
</html>
|