-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDB总结.htm
427 lines (368 loc) · 52.1 KB
/
MongoDB总结.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>MongoDB总结 | Anthony's Blog</title><meta name="keywords" content="Linux,Services,MongoDB"><meta name="author" content="HaoDong Wu"><meta name="copyright" content="HaoDong Wu"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Mongodb命令总结,避免遗忘。">
<meta property="og:type" content="article">
<meta property="og:title" content="MongoDB总结">
<meta property="og:url" content="https://www.scery.com/MongoDB%E6%80%BB%E7%BB%93.htm">
<meta property="og:site_name" content="Anthony's Blog">
<meta property="og:description" content="Mongodb命令总结,避免遗忘。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">
<meta property="article:published_time" content="2020-07-29T13:50:16.000Z">
<meta property="article:modified_time" content="2024-03-18T02:51:00.705Z">
<meta property="article:author" content="HaoDong Wu">
<meta property="article:tag" content="Linux">
<meta property="article:tag" content="Services">
<meta property="article:tag" content="MongoDB">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"><link rel="shortcut icon" href="/img/avatar.png"><link rel="canonical" href="https://www.scery.com/MongoDB%E6%80%BB%E7%BB%93.htm"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="baidu-site-verification" content="code-cOpY18SESu"/><link rel="stylesheet" href="css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
root: '/',
algolia: undefined,
localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容:${query}"}},
translate: undefined,
noticeOutdate: undefined,
highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
copy: {
success: '复制成功',
error: '复制错误',
noSupport: '浏览器不支持'
},
relativeDate: {
homepage: false,
post: false
},
runtime: '天',
date_suffix: {
just: '刚刚',
min: '分钟前',
hour: '小时前',
day: '天前',
month: '个月前'
},
copyright: undefined,
lightbox: 'fancybox',
Snackbar: undefined,
source: {
justifiedGallery: {
js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
}
},
isPhotoFigcaption: false,
islazyload: false,
isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
title: 'MongoDB总结',
isPost: true,
isHome: false,
isHighlightShrink: false,
isToc: true,
postUpdate: '2024-03-18 10:51:00'
}</script><noscript><style type="text/css">
#nav {
opacity: 1
}
.justified-gallery img {
opacity: 1
}
#recent-posts time,
#post-meta time {
display: inline !important
}
</style></noscript><script>(win=>{
win.saveToLocal = {
set: function setWithExpiry(key, value, ttl) {
if (ttl === 0) return
const now = new Date()
const expiryDay = ttl * 86400000
const item = {
value: value,
expiry: now.getTime() + expiryDay,
}
localStorage.setItem(key, JSON.stringify(item))
},
get: function getWithExpiry(key) {
const itemStr = localStorage.getItem(key)
if (!itemStr) {
return undefined
}
const item = JSON.parse(itemStr)
const now = new Date()
if (now.getTime() > item.expiry) {
localStorage.removeItem(key)
return undefined
}
return item.value
}
}
win.getScript = url => new Promise((resolve, reject) => {
const script = document.createElement('script')
script.src = url
script.async = true
script.onerror = reject
script.onload = script.onreadystatechange = function() {
const loadState = this.readyState
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
script.onload = script.onreadystatechange = null
resolve()
}
document.head.appendChild(script)
})
win.activateDarkMode = function () {
document.documentElement.setAttribute('data-theme', 'dark')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
}
}
win.activateLightMode = function () {
document.documentElement.setAttribute('data-theme', 'light')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
}
}
const t = saveToLocal.get('theme')
if (t === 'dark') activateDarkMode()
else if (t === 'light') activateLightMode()
const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
if (asideStatus === 'hide') {
document.documentElement.classList.add('hide-aside')
} else {
document.documentElement.classList.remove('hide-aside')
}
}
const detectApple = () => {
if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
document.documentElement.classList.add('apple')
}
}
detectApple()
})(window)</script><meta name="generator" content="Hexo 5.4.2"><link rel="alternate" href="atom.xml" title="Anthony's Blog" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="../img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="../archives/"><div class="headline">文章</div><div class="length-num">61</div></a><a href="../tags/"><div class="headline">标签</div><div class="length-num">58</div></a><a href="/"><div class="headline">分类</div><div class="length-num">61</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="../index.html"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="../archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="../tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="../categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="../links/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div><div class="menus_item"><a class="site-page" href="../about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7')"><nav id="nav"><span id="blog_name"><a id="site-name" href="../index.html">Anthony's Blog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="../index.html"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="../archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="../tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="../categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="../links/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div><div class="menus_item"><a class="site-page" href="../about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">MongoDB总结</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-07-29T13:50:16.000Z" title="发表于 2020-07-29 21:50:16">2020-07-29</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-03-18T02:51:00.705Z" title="更新于 2024-03-18 10:51:00">2024-03-18</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="categories/Linux/">Linux</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="categories/Linux/MongoDB/">MongoDB</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="categories/Linux/MongoDB/%E5%91%BD%E4%BB%A4/">命令</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="categories/Linux/MongoDB/%E5%91%BD%E4%BB%A4/%E4%BD%BF%E7%94%A8/">使用</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>6分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="MongoDB总结"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="MongoDB总结"><a href="#MongoDB总结" class="headerlink" title="MongoDB总结"></a>MongoDB总结</h1><h3 id="基本操作"><a href="#基本操作" class="headerlink" title="基本操作"></a>基本操作</h3><figure class="highlight stata"><table><tr><td class="code"><pre><span class="line">show databases;</span><br><span class="line"></span><br><span class="line"><span class="keyword">use</span> databasename;</span><br><span class="line"></span><br><span class="line"><span class="keyword">db</span>.dropDatabase();</span><br><span class="line"></span><br><span class="line">show collections;</span><br><span class="line"></span><br><span class="line"><span class="keyword">db</span>.createCollection('collectionname');</span><br><span class="line"></span><br><span class="line"><span class="keyword">db</span>.collectionname.<span class="keyword">drop</span>();</span><br></pre></td></tr></table></figure>
<h3 id="CURD"><a href="#CURD" class="headerlink" title="CURD"></a>CURD</h3><h4 id="增:"><a href="#增:" class="headerlink" title="增:"></a>增:</h4><p>集合存在则直接插入数据,不存在则隐式创建</p>
<p>collections为集合名</p>
<figure class="highlight stylus"><table><tr><td class="code"><pre><span class="line">db<span class="selector-class">.collection</span><span class="selector-class">.insert</span>({name:<span class="string">"jack"</span>}) <span class="comment">//单个JSON插入</span></span><br><span class="line">db<span class="selector-class">.collection</span><span class="selector-class">.insert</span>(<span class="selector-attr">[{name:<span class="string">"jack"</span>}.{name:<span class="string">"tom"</span>}]</span>) <span class="comment">//多个JSON插入</span></span><br><span class="line"><span class="function"><span class="title">for</span><span class="params">(var i =<span class="number">0</span>;i<<span class="number">5</span>;i++)</span></span>{</span><br><span class="line">db<span class="selector-class">.collection</span><span class="selector-class">.insert</span>({name:<span class="string">"jack"</span>})</span><br><span class="line">}<span class="comment">//快速插入多个数据</span></span><br></pre></td></tr></table></figure>
<h4 id="改"><a href="#改" class="headerlink" title="改:"></a>改:</h4><figure class="highlight xquery"><table><tr><td class="code"><pre><span class="line">db<span class="built_in">.collection</span>.<span class="keyword">update</span>(条件,新数据[,是否新增,是否修改多条数据])</span><br><span class="line">//是否新增<span class="built_in">:true</span>则为插入,默认flase为不插入</span><br><span class="line">//是否修改多条:匹配到的数据都修改,默<span class="built_in">认true</span>为是<span class="built_in">,false</span>为否</span><br><span class="line">db<span class="built_in">.collection</span>.<span class="keyword">update</span>(<span class="built_in">{name</span>:<span class="string">"tom"</span>},<span class="built_in">{name</span>:<span class="string">"saily"</span>}<span class="built_in">,false</span><span class="built_in">,true</span>)</span><br><span class="line">//这样子为将tom的那条数据替换成<span class="built_in">{name</span>:<span class="string">"saily"</span>},默认为替换不是修改</span><br></pre></td></tr></table></figure>
<p><strong>下面运算符可在新数据里面使用</strong></p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>$inc</td>
<td>递增</td>
</tr>
<tr>
<td>$rename</td>
<td>重命名列</td>
</tr>
<tr>
<td>$set</td>
<td>修改列值</td>
</tr>
<tr>
<td>$unset</td>
<td>删除列</td>
</tr>
</tbody></table>
<figure class="highlight xquery"><table><tr><td class="code"><pre><span class="line">//<span class="variable">$inc</span>使用,将age增加<span class="number">3</span></span><br><span class="line">db<span class="built_in">.collection</span>.<span class="keyword">update</span>(<span class="built_in">{name</span>:<span class="string">"tom"</span>},{<span class="variable">$inc:</span>{age: <span class="number">3</span>}})</span><br><span class="line">//<span class="variable">$inc</span>使用,将age减少<span class="number">3</span></span><br><span class="line">db<span class="built_in">.collection</span>.<span class="keyword">update</span>(<span class="built_in">{name</span>:<span class="string">"tom"</span>},{<span class="variable">$inc:</span>{age: -<span class="number">3</span>}})</span><br></pre></td></tr></table></figure>
<figure class="highlight xquery"><table><tr><td class="code"><pre><span class="line">//<span class="variable">$rename</span>使用,修<span class="built_in">改name</span>命名为uname</span><br><span class="line">db<span class="built_in">.collection</span>.<span class="keyword">update</span>(<span class="built_in">{name</span>:<span class="string">"tom"</span>},{<span class="variable">$rename:</span><span class="built_in">{name</span>:<span class="string">"uname"</span>}})</span><br></pre></td></tr></table></figure>
<figure class="highlight xquery"><table><tr><td class="code"><pre><span class="line">//<span class="variable">$set</span>使用,将修<span class="built_in">改name</span>为saily</span><br><span class="line">db<span class="built_in">.collection</span>.<span class="keyword">update</span>(<span class="built_in">{name</span>:<span class="string">"tom"</span>},{<span class="variable">$set:</span><span class="built_in">{name</span>:<span class="string">"saily"</span>}})</span><br></pre></td></tr></table></figure>
<figure class="highlight xquery"><table><tr><td class="code"><pre><span class="line">//<span class="variable">$unset</span>使用,将age字段删除<span class="built_in">,true</span>为删除</span><br><span class="line">db<span class="built_in">.collection</span>.<span class="keyword">update</span>(<span class="built_in">{name</span>:<span class="string">"tom"</span>},{<span class="variable">$unset:</span>{age:true}})</span><br></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="code"><pre><span class="line"><span class="comment">//一次性写多个修改器</span></span><br><span class="line">db.collection.<span class="title function_ invoke__">update</span>({<span class="attr">name</span>:<span class="string">"tom"</span>},{</span><br><span class="line"><span class="variable">$inc</span>:{<span class="attr">age</span>: <span class="number">3</span>},</span><br><span class="line"><span class="variable">$set</span>:{<span class="attr">name</span>:<span class="string">"saily"</span>},</span><br><span class="line"><span class="variable">$rename</span>:{<span class="attr">name</span>:<span class="string">"uname"</span>},</span><br><span class="line"><span class="variable">$unset</span>:{<span class="attr">age</span>:<span class="literal">true</span>}</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h4 id="查:"><a href="#查:" class="headerlink" title="查:"></a>查:</h4><figure class="highlight sqf"><table><tr><td class="code"><pre><span class="line">db.collection.<span class="built_in">find</span>(条件[,查询的列])</span><br><span class="line"><span class="comment">//格式化查询文档</span></span><br><span class="line">db.collection.<span class="built_in">find</span>().pretty();</span><br><span class="line"></span><br><span class="line"><span class="comment">//查询age为6的数据</span></span><br><span class="line">db.collection.<span class="built_in">find</span>({age:<span class="number">6</span>})</span><br><span class="line"></span><br><span class="line"><span class="comment">//查询age为6和sex为male的数据</span></span><br><span class="line">db.collection.<span class="built_in">find</span>({age:<span class="number">6</span>,sex:<span class="string">"male"</span>})</span><br><span class="line"></span><br><span class="line"><span class="comment">//查询的列 表示 结果筛选</span></span><br><span class="line"><span class="comment">//查询name为tom只显示age字段</span></span><br><span class="line">db.collection.<span class="built_in">find</span>({<span class="built_in">name</span>:<span class="string">"tom"</span>},{age:<span class="number">1</span>})</span><br><span class="line"></span><br><span class="line"><span class="comment">//查询name为tom除了age字段都显示</span></span><br><span class="line">db.collection.<span class="built_in">find</span>({<span class="built_in">name</span>:<span class="string">"tom"</span>},{age:<span class="number">0</span>})</span><br></pre></td></tr></table></figure>
<h4 id="删:"><a href="#删:" class="headerlink" title="删:"></a>删:</h4><figure class="highlight stylus"><table><tr><td class="code"><pre><span class="line">db<span class="selector-class">.collection</span><span class="selector-class">.remove</span>(条件<span class="selector-attr">[,是否删除一条]</span>)</span><br><span class="line"><span class="comment">//删除集合中所有文档</span></span><br><span class="line">db<span class="selector-class">.collection</span><span class="selector-class">.remove</span>({})</span><br><span class="line"><span class="comment">//是否删除一条,只删除一条为true,默认为否删除全部false</span></span><br></pre></td></tr></table></figure>
<h3 id="排序:"><a href="#排序:" class="headerlink" title="排序:"></a>排序:</h3><figure class="highlight stata"><table><tr><td class="code"><pre><span class="line"><span class="comment">//JSON数据里面根据键值排序,键为字段名,值为1则升序排序,值为-1则为降序排序</span></span><br><span class="line"><span class="keyword">db</span>.collectionname.find().<span class="keyword">sort</span>(JSON数据)</span><br><span class="line"></span><br><span class="line"><span class="comment">//年龄按降序排序</span></span><br><span class="line"><span class="keyword">db</span>.<span class="keyword">test</span>.find().<span class="keyword">sort</span>({age: -1});</span><br></pre></td></tr></table></figure>
<h3 id="Limit与Skip方法:"><a href="#Limit与Skip方法:" class="headerlink" title="Limit与Skip方法:"></a>Limit与Skip方法:</h3><figure class="highlight scss"><table><tr><td class="code"><pre><span class="line">db<span class="selector-class">.collectionname</span><span class="selector-class">.find</span>()<span class="selector-class">.sort</span>()<span class="selector-class">.skip</span>(数字)<span class="selector-class">.limit</span>(数字);</span><br><span class="line"><span class="comment">//skip跳过指定数量文档(可选),limit限制查询的数量</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//降序查询2条数据</span></span><br><span class="line">db<span class="selector-class">.test</span><span class="selector-class">.find</span>()<span class="selector-class">.sort</span>({age:-<span class="number">1</span>})<span class="selector-class">.limit</span>(<span class="number">2</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//降序查询2条而且跳过2条数据</span></span><br><span class="line">db<span class="selector-class">.test</span><span class="selector-class">.find</span>()<span class="selector-class">.sort</span>({age:-<span class="number">1</span>})<span class="selector-class">.skip</span>(<span class="number">2</span>)<span class="selector-class">.limit</span>(<span class="number">2</span>);</span><br></pre></td></tr></table></figure>
<h3 id="聚合查询:"><a href="#聚合查询:" class="headerlink" title="聚合查询:"></a>聚合查询:</h3><figure class="highlight dust"><table><tr><td class="code"><pre><span class="line"><span class="language-xml">db.collectionname.aggregate([</span></span><br><span class="line"><span class="language-xml"> </span><span class="template-variable">{管道:{表达式}</span><span class="language-xml">}</span></span><br><span class="line"><span class="language-xml"> ...</span></span><br><span class="line"><span class="language-xml">])</span></span><br></pre></td></tr></table></figure>
<p><strong>常用管道</strong></p>
<table>
<thead>
<tr>
<th>方法</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>$group</td>
<td>将集合中的文档分组,用于统计结果</td>
</tr>
<tr>
<td>$match</td>
<td>过滤数据,只要输出符合条件的文档</td>
</tr>
<tr>
<td>$sort</td>
<td>聚合数据进一步排序</td>
</tr>
<tr>
<td>$skip</td>
<td>跳过指定的文档数</td>
</tr>
<tr>
<td>$limit</td>
<td>限制集合数据返回文档数</td>
</tr>
<tr>
<td>…</td>
<td>…</td>
</tr>
</tbody></table>
<p><strong>常用表达式</strong></p>
<table>
<thead>
<tr>
<th align="left">方法</th>
<th>作用</th>
<th>用法</th>
</tr>
</thead>
<tbody><tr>
<td align="left">$sum</td>
<td>总和</td>
<td>$sum:1 同count表示统计</td>
</tr>
<tr>
<td align="left">$avg</td>
<td>平均</td>
<td></td>
</tr>
<tr>
<td align="left">$min</td>
<td>最小值</td>
<td></td>
</tr>
<tr>
<td align="left">$max</td>
<td>最大值</td>
<td></td>
</tr>
<tr>
<td align="left">…</td>
<td>…</td>
<td>…</td>
</tr>
</tbody></table>
<figure class="highlight awk"><table><tr><td class="code"><pre><span class="line"><span class="regexp">//</span>统计sex的总人数</span><br><span class="line">db.test.aggregate([</span><br><span class="line"> {</span><br><span class="line"> <span class="variable">$group</span>:{</span><br><span class="line"> _id: <span class="string">"$sex"</span>, <span class="regexp">//</span>按照sex列的顺序分组</span><br><span class="line"> rs: {<span class="variable">$sum</span>:<span class="number">1</span>} <span class="regexp">//</span>rs为统计总和的结果</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">])</span><br></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="code"><pre><span class="line"><span class="comment">//统计所有人数的平均年龄</span></span><br><span class="line">db.test.<span class="title function_ invoke__">aggregate</span>([</span><br><span class="line"> {<span class="variable">$group</span>:{</span><br><span class="line"> <span class="attr">_id</span>: <span class="literal">null</span>,</span><br><span class="line"> <span class="attr">totalNum</span>: {<span class="variable">$sum</span>: <span class="number">1</span>},</span><br><span class="line"> <span class="attr">avgAge</span>: {<span class="variable">$avg</span>: <span class="string">"<span class="subst">$age</span>"</span>}</span><br><span class="line"> }}</span><br><span class="line">])</span><br></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="code"><pre><span class="line"><span class="comment">//查询每个sex的人数,按人数升序排序</span></span><br><span class="line">db.test.<span class="title function_ invoke__">aggregate</span>([</span><br><span class="line"> {</span><br><span class="line"> <span class="variable">$group</span>:{</span><br><span class="line"> <span class="attr">_id</span>: <span class="string">"<span class="subst">$sex</span>"</span>,</span><br><span class="line"> <span class="attr">totalNum</span>: {<span class="variable">$sum</span>:<span class="number">1</span>},</span><br><span class="line"> }</span><br><span class="line"> },</span><br><span class="line"> {</span><br><span class="line"> <span class="variable">$sort</span>:{<span class="attr">totalNum</span>:<span class="number">1</span>}</span><br><span class="line"> }</span><br><span class="line">])</span><br></pre></td></tr></table></figure>
<h3 id="索引:"><a href="#索引:" class="headerlink" title="索引:"></a>索引:</h3><figure class="highlight stylus"><table><tr><td class="code"><pre><span class="line"><span class="comment">//创建索引语法</span></span><br><span class="line">db<span class="selector-class">.collectionname</span><span class="selector-class">.createIndex</span>(待创建索引的列<span class="selector-attr">[,额外选项]</span>)</span><br><span class="line"><span class="comment">//待创建索引的列: {键:1,...,键:-1} 1为按升序创建索引,-1位按降序创建索引</span></span><br><span class="line"><span class="comment">//额外选项:设置索引的名称或者唯一索引等 {name:indexname,unique:列名}</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//删除索引语法</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//全部删除</span></span><br><span class="line">db<span class="selector-class">.collectionname</span><span class="selector-class">.dropIndexes</span>()</span><br><span class="line"><span class="comment">//部分删除</span></span><br><span class="line">db<span class="selector-class">.collectionname</span><span class="selector-class">.dropIndex</span>(<span class="string">'索引名'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">//查看索引语法</span></span><br><span class="line">db<span class="selector-class">.collectionname</span><span class="selector-class">.getIndexes</span>()</span><br></pre></td></tr></table></figure>
<figure class="highlight reasonml"><table><tr><td class="code"><pre><span class="line"><span class="comment">//创建多个索引</span></span><br><span class="line">db.collectionname.create<span class="constructor">Index({<span class="params">name</span>:1,<span class="params">age</span>:1})</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//创建唯一索引</span></span><br><span class="line">db.collectionname.create<span class="constructor">Index({<span class="params">name</span>:1},{<span class="params">unique</span>:<span class="params">age</span>})</span></span><br></pre></td></tr></table></figure>
<h3 id="分析索引:"><a href="#分析索引:" class="headerlink" title="分析索引:"></a>分析索引:</h3><figure class="highlight awk"><table><tr><td class="code"><pre><span class="line">db.collectionname.find().explain(<span class="string">'executionStats'</span>);</span><br><span class="line"></span><br><span class="line"><span class="regexp">//</span>COLLSCAN 全表扫描</span><br><span class="line"><span class="regexp">//</span>IXSCAN 索引扫描</span><br><span class="line"><span class="regexp">//</span>FETCH 根据索引去检索指定的document</span><br></pre></td></tr></table></figure>
<p><img src="https://pic-1257960926.cos.ap-guangzhou.myqcloud.com/blog/IMG_0512-6029409.jpg" alt="IMG_0512"></p>
<figure class="highlight sqf"><table><tr><td class="code"><pre><span class="line"><span class="comment">//未添加索引寻找</span></span><br><span class="line">db.test.<span class="built_in">find</span>({<span class="built_in">name</span>:<span class="string">"tom"</span>}).explain(<span class="string">'executionStats'</span>);</span><br><span class="line"><span class="comment">//添加索引寻找</span></span><br><span class="line">db.test.<span class="built_in">find</span>({<span class="built_in">name</span>:<span class="string">"tom"</span>}).explain(<span class="string">'executionStats'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="权限机制:"><a href="#权限机制:" class="headerlink" title="权限机制:"></a>权限机制:</h3><figure class="highlight php"><table><tr><td class="code"><pre><span class="line"><span class="keyword">use</span> <span class="title">admin</span>;</span><br><span class="line">db.<span class="title function_ invoke__">createUser</span>({</span><br><span class="line"> <span class="string">"user"</span>: <span class="string">"huangjiahui"</span>,</span><br><span class="line"> <span class="string">"pwd"</span>: <span class="string">"huangjiahui"</span>,</span><br><span class="line"> <span class="attr">roles</span>:[{</span><br><span class="line"> <span class="attr">role</span>: <span class="string">"readWrite"</span>,</span><br><span class="line"> <span class="attr">db</span>: <span class="string">"huangjiahui"</span></span><br><span class="line"> }]</span><br><span class="line">});</span><br><span class="line"><span class="comment">//或者</span></span><br><span class="line">db.<span class="title function_ invoke__">createUser</span>({</span><br><span class="line"> <span class="string">"user"</span>: <span class="string">"账号"</span>,</span><br><span class="line"> <span class="string">"pwd"</span>: <span class="string">"密码"</span>,</span><br><span class="line"> <span class="attr">roles</span>:[{</span><br><span class="line"> <span class="attr">role</span>: <span class="string">"角色"</span>,</span><br><span class="line"> <span class="attr">db</span>: <span class="string">"所属数据库"</span></span><br><span class="line"> },{</span><br><span class="line"> <span class="attr">role</span>: <span class="string">"readWrite"</span>,</span><br><span class="line"> <span class="attr">db</span>: <span class="string">"zdnfcj"</span></span><br><span class="line"> },</span><br><span class="line"> ]</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line"><span class="comment">//指定某个数据库创建用户,要到当下的数据库下创建用户</span></span><br><span class="line"><span class="comment">//admin下的system.users集合能看到所有用户</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>#角色种类</p>
<p>超级用户角色:root</p>
<p>数据库用户角色:read、readWrite</p>
<p>数据库管理角色:dbAdmin、userAdmin</p>
<p>集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager</p>
<p>备份恢复角色:backup、restore</p>
<p>所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase</p>
</blockquote>
<p><strong>通过root登录</strong></p>
<ul>
<li>mongo ip:port/database -u user -p pwd</li>
<li>先登录 mongo,选择admin数据库,db.auth(user,pwd)</li>
</ul>
<h3 id="备份还原"><a href="#备份还原" class="headerlink" title="备份还原:"></a>备份还原:</h3><p><strong>备份</strong></p>
<figure class="highlight diff"><table><tr><td class="code"><pre><span class="line">mongodump -h -port -u -p -d -o</span><br><span class="line"></span><br><span class="line">//语法说明</span><br><span class="line"><span class="deletion">-h host ip</span></span><br><span class="line"><span class="deletion">-port port</span></span><br><span class="line"><span class="deletion">-u user</span></span><br><span class="line"><span class="deletion">-p pwd</span></span><br><span class="line"><span class="deletion">-d database</span></span><br><span class="line"><span class="deletion">-o open ./xxx/ 备份路径文件夹</span></span><br></pre></td></tr></table></figure>
<p><strong>还原</strong></p>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line">mongorestore -h -port -u -<span class="selector-tag">p</span> -d <span class="attr">--drop</span> 备份数据目录</span><br><span class="line"></span><br><span class="line">-d 不写则还原全部数据</span><br><span class="line"><span class="attr">--drop</span> 先删除数据库再导入</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://www.scery.com">HaoDong Wu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://www.scery.com/MongoDB%E6%80%BB%E7%BB%93.htm">https://www.scery.com/MongoDB%E6%80%BB%E7%BB%93.htm</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://www.scery.com" target="_blank">Anthony's Blog</a>!</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="tags/Linux/">Linux</a><a class="post-meta__tags" href="tags/Services/">Services</a><a class="post-meta__tags" href="tags/MongoDB/">MongoDB</a></div><div class="post_share"><div class="social-share" data-image="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="MongoDB%E9%9B%86%E7%BE%A4-%E5%89%AF%E6%9C%AC%E9%9B%86.htm"><img class="prev-cover" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">MongoDB集群-副本集</div></div></a></div><div class="next-post pull-right"><a href="diskutil.htm"><img class="next-cover" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Diskutil工具集使用</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="MongoDB%E6%8C%87%E4%BB%A4.htm" title="MongoDB的命令详解"><img class="cover" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-09</div><div class="title">MongoDB的命令详解</div></div></a></div><div><a href="MongoDB%E9%9B%86%E7%BE%A4-%E5%89%AF%E6%9C%AC%E9%9B%86.htm" title="MongoDB集群-副本集"><img class="cover" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-07-30</div><div class="title">MongoDB集群-副本集</div></div></a></div><div><a href="Nodejs%E6%93%8D%E4%BD%9CMongoDB.htm" title="Nodejs操作MongoDB"><img class="cover" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-01</div><div class="title">Nodejs操作MongoDB</div></div></a></div><div><a href="nmap%E7%9A%84%E5%91%BD%E4%BB%A4%E8%AF%A6%E8%A7%A3.htm" title="nmap的命令详解"><img class="cover" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-03-09</div><div class="title">nmap的命令详解</div></div></a></div><div><a href="Centos7_firewalld.htm" title="Centos7_firewalld"><img class="cover" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-02-20</div><div class="title">Centos7_firewalld</div></div></a></div><div><a href="Centos7%E5%AE%89%E8%A3%85mysql8.0.htm" title="Centos7安装mysql8.0和日常命令"><img class="cover" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-02-20</div><div class="title">Centos7安装mysql8.0和日常命令</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="gitalk-container"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="../img/avatar.png" onerror="this.onerror=null;this.src='../img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">HaoDong Wu</div><div class="author-info__description">Email: [email protected]</div></div><div class="card-info-data site-data is-center"><a href="../archives/"><div class="headline">文章</div><div class="length-num">61</div></a><a href="../tags/"><div class="headline">标签</div><div class="length-num">58</div></a><a href="/"><div class="headline">分类</div><div class="length-num">61</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/Anthonyfhd"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Anthonyfhd" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="../wuhaodong:[email protected]" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="https://weibo.com/u/5730157238" target="_blank" title="z"><i class="fab fa-weibo"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">I am a slow walker,but I never walk backwards.</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#MongoDB%E6%80%BB%E7%BB%93"><span class="toc-number">1.</span> <span class="toc-text">MongoDB总结</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C"><span class="toc-number">1.0.1.</span> <span class="toc-text">基本操作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CURD"><span class="toc-number">1.0.2.</span> <span class="toc-text">CURD</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%A2%9E%EF%BC%9A"><span class="toc-number">1.0.2.1.</span> <span class="toc-text">增:</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%94%B9"><span class="toc-number">1.0.2.2.</span> <span class="toc-text">改:</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9F%A5%EF%BC%9A"><span class="toc-number">1.0.2.3.</span> <span class="toc-text">查:</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%A0%EF%BC%9A"><span class="toc-number">1.0.2.4.</span> <span class="toc-text">删:</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8E%92%E5%BA%8F%EF%BC%9A"><span class="toc-number">1.0.3.</span> <span class="toc-text">排序:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Limit%E4%B8%8ESkip%E6%96%B9%E6%B3%95%EF%BC%9A"><span class="toc-number">1.0.4.</span> <span class="toc-text">Limit与Skip方法:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%81%9A%E5%90%88%E6%9F%A5%E8%AF%A2%EF%BC%9A"><span class="toc-number">1.0.5.</span> <span class="toc-text">聚合查询:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B4%A2%E5%BC%95%EF%BC%9A"><span class="toc-number">1.0.6.</span> <span class="toc-text">索引:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E6%9E%90%E7%B4%A2%E5%BC%95%EF%BC%9A"><span class="toc-number">1.0.7.</span> <span class="toc-text">分析索引:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9D%83%E9%99%90%E6%9C%BA%E5%88%B6%EF%BC%9A"><span class="toc-number">1.0.8.</span> <span class="toc-text">权限机制:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%87%E4%BB%BD%E8%BF%98%E5%8E%9F"><span class="toc-number">1.0.9.</span> <span class="toc-text">备份还原:</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="../XT20%E4%BD%BF%E7%94%A8%E6%96%B0%E6%9C%BA%E5%9E%8B%E7%9A%84%E9%A2%84%E8%AE%BE.htm" title="无题">无题</a><time datetime="2025-01-04T04:30:41.696Z" title="发表于 2025-01-04 12:30:41">2025-01-04</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="../%E7%BB%84%E4%BC%9A/20241223%E7%BB%84%E4%BC%9A.htm" title="20241223组会">20241223组会</a><time datetime="2024-12-23T06:56:00.000Z" title="发表于 2024-12-23 14:56:00">2024-12-23</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="../%E7%B2%BE%E5%87%86%E5%9B%BD%E9%99%85%E4%BC%A0%E6%92%AD%E7%B3%BB%E7%BB%9F/%E7%B2%BE%E4%BC%A0%E7%B3%BB%E7%BB%9F%E9%A1%B5%E9%9D%A2%E6%9D%83%E9%99%90%E9%85%8D%E7%BD%AE.htm" title="精传系统页面权限配置">精传系统页面权限配置</a><time datetime="2024-12-16T00:48:25.000Z" title="发表于 2024-12-16 08:48:25">2024-12-16</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="../%E5%B2%B3%E9%BA%93%E5%B1%B1%E6%95%B0%E6%99%BA%E4%BC%A0%E6%92%AD%E5%AE%9E%E9%AA%8C%E5%AE%A4/%E5%B2%B3%E9%BA%93%E5%B1%B1%E6%95%B0%E6%99%BA%E4%BC%A0%E6%92%AD%201.htm" title="岳麓山数智传播 1">岳麓山数智传播 1</a><time datetime="2024-12-15T08:54:00.000Z" title="发表于 2024-12-15 16:54:00">2024-12-15</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="../%E5%B2%B3%E9%BA%93%E5%B1%B1%E6%95%B0%E6%99%BA%E4%BC%A0%E6%92%AD%E5%AE%9E%E9%AA%8C%E5%AE%A4/%E5%B2%B3%E9%BA%93%E5%B1%B1%E6%95%B0%E6%99%BA%E4%BC%A0%E6%92%AD.htm" title="岳麓山数智传播">岳麓山数智传播</a><time datetime="2024-12-12T07:10:00.000Z" title="发表于 2024-12-12 15:10:00">2024-12-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">©2020 - 2025 By HaoDong Wu</div><div class="framework-info"></div><div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">粤ICP备2024239461号 </a>
<img src="/images/beian.png" style="display: inline-block;"><a href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=none" rel="noopener" target="_blank">粤公网安备号 </a>
</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span> 数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="js/utils.js"></script><script src="js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="js/search/local-search.js"></script><script>var preloader = {
endLoading: () => {
document.body.style.overflow = 'auto';
document.getElementById('loading-box').classList.add("loaded")
},
initLoading: () => {
document.body.style.overflow = '';
document.getElementById('loading-box').classList.remove("loaded")
}
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>if (!window.MathJax) {
window.MathJax = {
tex: {
inlineMath: [ ['$','$'], ["\\(","\\)"]],
tags: 'ams'
},
chtml: {
scale: 1.2
},
options: {
renderActions: {
findScript: [10, doc => {
for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
const display = !!node.type.match(/; *mode=display/)
const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
const text = document.createTextNode('')
node.parentNode.replaceChild(text, node)
math.start = {node: text, delim: '', n: 0}
math.end = {node: text, delim: '', n: 0}
doc.math.push(math)
}
}, ''],
insertScript: [200, () => {
document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
const target = node.parentNode
if (target.nodeName.toLowerCase() === 'li') {
target.parentNode.classList.add('has-jax')
} else {
target.classList.add('has-jax')
}
});
}, '', false]
}
}
}
const script = document.createElement('script')
script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
script.id = 'MathJax-script'
script.async = true
document.head.appendChild(script)
} else {
MathJax.startup.document.state(0)
MathJax.texReset()
MathJax.typeset()
}</script><script>function addGitalkSource () {
const ele = document.createElement('link')
ele.rel = 'stylesheet'
ele.href= 'https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.css'
document.getElementsByTagName('head')[0].appendChild(ele)
}
function loadGitalk () {
function initGitalk () {
var gitalk = new Gitalk(Object.assign({
clientID: '85556d73db8de5db1c7f',
clientSecret: 'c62529657d2e66f49a40272e9aa404225a1952c3',
repo: 'Blog_GitTalk',
owner: 'Anthonyfhd',
admin: ['Anthonyfhd'],
id: 'd41d8cd98f00b204e9800998ecf8427e',
updateCountCallback: commentCount
},null))
gitalk.render('gitalk-container')
}
if (typeof Gitalk === 'function') initGitalk()
else {
addGitalkSource()
getScript('https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.js').then(initGitalk)
}
}
function commentCount(n){
let isCommentCount = document.querySelector('#post-meta .gitalk-comment-count')
if (isCommentCount) {
isCommentCount.innerHTML= n
}
}
if ('Gitalk' === 'Gitalk' || !true) {
if (true) btf.loadComment(document.getElementById('gitalk-container'), loadGitalk)
else loadGitalk()
} else {
function loadOtherComment () {
loadGitalk()
}
}</script></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/fireworks.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>