-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoose.html
1200 lines (823 loc) · 85.9 KB
/
Mongoose.html
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html style="display: none;" lang="zh">
<head>
<meta charset="utf-8">
<!--
© Material Theme
https://github.com/viosey/hexo-theme-material
Version: 1.5.2 -->
<script>
window.materialVersion = "1.5.2"
// Delete localstorage with these tags
window.oldVersion = [
'codestartv1',
'1.3.4',
'1.4.0',
'1.4.0b1',
'1.5.0'
]
</script>
<!-- dns prefetch -->
<meta http-equiv="x-dns-prefetch-control" content="on">
<link rel="dns-prefetch" href="https://cdn1.lncld.net"/>
<!-- Meta & Info -->
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="renderer" content="webkit">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!-- Title -->
<title>
Mongoose |
黄明照--一个在路上慢慢行走的前端人
</title>
<!-- Favicons -->
<link rel="icon shortcut" type="image/ico" href="/img/favicon.png">
<link rel="icon" href="/img/favicon.png">
<meta name="format-detection" content="telephone=no"/>
<meta name="description" itemprop="description" content="黄明照、黄明照的个人博客、黄明照的个人网站、一个在路上慢慢行走的前端人">
<meta name="keywords" content="黄明照、黄明照的个人博客、黄明照的个人网站、一个在路上慢慢行走的前端人,Mongoose">
<meta name="theme-color" content="#0097A7">
<!-- Disable Fucking Bloody Baidu Tranformation -->
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lte IE 9]>
<link rel="stylesheet" href="/css/ie-blocker.css">
<script src="/js/ie-blocker.zhCN.js"></script>
<![endif]-->
<!-- Import lsloader -->
<script>(function(){window.lsloader={jsRunSequence:[],jsnamemap:{},cssnamemap:{}};lsloader.removeLS=function(a){try{localStorage.removeItem(a)}catch(b){}};lsloader.setLS=function(a,c){try{localStorage.setItem(a,c)}catch(b){}};lsloader.getLS=function(a){var c="";try{c=localStorage.getItem(a)}catch(b){c=""}return c};versionString="/*"+(window.materialVersion||"unknownVersion")+"*/";lsloader.clean=function(){try{var b=[];for(var a=0;a<localStorage.length;a++){b.push(localStorage.key(a))}b.forEach(function(e){var f=lsloader.getLS(e);if(window.oldVersion){var d=window.oldVersion.reduce(function(g,h){return g||f.indexOf("/*"+h+"*/")!==-1},false);if(d){lsloader.removeLS(e)}}})}catch(c){}};lsloader.clean();lsloader.load=function(f,a,b,d){if(typeof b==="boolean"){d=b;b=undefined}d=d||false;b=b||function(){};var e;e=this.getLS(f);if(e&&e.indexOf(versionString)===-1){this.removeLS(f);this.requestResource(f,a,b,d);return}if(e){var c=e.split(versionString)[0];if(c!=a){console.log("reload:"+a);this.removeLS(f);this.requestResource(f,a,b,d);return}e=e.split(versionString)[1];if(d){this.jsRunSequence.push({name:f,code:e});this.runjs(a,f,e)}else{document.getElementById(f).appendChild(document.createTextNode(e));b()}}else{this.requestResource(f,a,b,d)}};lsloader.requestResource=function(b,e,a,c){var d=this;if(c){this.iojs(e,b,function(h,f,g){d.setLS(f,h+versionString+g);d.runjs(h,f,g)})}else{this.iocss(e,b,function(f){document.getElementById(b).appendChild(document.createTextNode(f));d.setLS(b,e+versionString+f)},a)}};lsloader.iojs=function(d,b,g){var a=this;a.jsRunSequence.push({name:b,code:""});try{var f=new XMLHttpRequest();f.open("get",d,true);f.onreadystatechange=function(){if(f.readyState==4){if((f.status>=200&&f.status<300)||f.status==304){if(f.response!=""){g(d,b,f.response);return}}a.jsfallback(d,b)}};f.send(null)}catch(c){a.jsfallback(d,b)}};lsloader.iocss=function(f,c,h,a){var b=this;try{var g=new XMLHttpRequest();g.open("get",f,true);g.onreadystatechange=function(){if(g.readyState==4){if((g.status>=200&&g.status<300)||g.status==304){if(g.response!=""){h(g.response);a();return}}b.cssfallback(f,c,a)}};g.send(null)}catch(d){b.cssfallback(f,c,a)}};lsloader.iofonts=function(f,c,h,a){var b=this;try{var g=new XMLHttpRequest();g.open("get",f,true);g.onreadystatechange=function(){if(g.readyState==4){if((g.status>=200&&g.status<300)||g.status==304){if(g.response!=""){h(g.response);a();return}}b.cssfallback(f,c,a)}};g.send(null)}catch(d){b.cssfallback(f,c,a)}};lsloader.runjs=function(f,c,e){if(!!c&&!!e){for(var b in this.jsRunSequence){if(this.jsRunSequence[b].name==c){this.jsRunSequence[b].code=e}}}if(!!this.jsRunSequence[0]&&!!this.jsRunSequence[0].code&&this.jsRunSequence[0].status!="failed"){var a=document.createElement("script");a.appendChild(document.createTextNode(this.jsRunSequence[0].code));a.type="text/javascript";document.getElementsByTagName("head")[0].appendChild(a);this.jsRunSequence.shift();if(this.jsRunSequence.length>0){this.runjs()}}else{if(!!this.jsRunSequence[0]&&this.jsRunSequence[0].status=="failed"){var d=this;var a=document.createElement("script");a.src=this.jsRunSequence[0].path;a.type="text/javascript";this.jsRunSequence[0].status="loading";a.onload=function(){d.jsRunSequence.shift();if(d.jsRunSequence.length>0){d.runjs()}};document.body.appendChild(a)}}};lsloader.tagLoad=function(b,a){this.jsRunSequence.push({name:a,code:"",path:b,status:"failed"});this.runjs()};lsloader.jsfallback=function(c,b){if(!!this.jsnamemap[b]){return}else{this.jsnamemap[b]=b}for(var a in this.jsRunSequence){if(this.jsRunSequence[a].name==b){this.jsRunSequence[a].code="";this.jsRunSequence[a].status="failed";this.jsRunSequence[a].path=c}}this.runjs()};lsloader.cssfallback=function(e,c,b){if(!!this.cssnamemap[c]){return}else{this.cssnamemap[c]=1}var d=document.createElement("link");d.type="text/css";d.href=e;d.rel="stylesheet";d.onload=d.onerror=b;var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(d,a)};lsloader.runInlineScript=function(c,b){var a=document.getElementById(b).innerText;this.jsRunSequence.push({name:c,code:a});this.runjs()}})();</script>
<!-- Import queue -->
<script>function Queue(){this.dataStore=[];this.offer=b;this.poll=d;this.execNext=a;this.debug=false;this.startDebug=c;function b(e){if(this.debug){console.log("Offered a Queued Function.")}if(typeof e==="function"){this.dataStore.push(e)}else{console.log("You must offer a function.")}}function d(){if(this.debug){console.log("Polled a Queued Function.")}return this.dataStore.shift()}function a(){var e=this.poll();if(e!==undefined){if(this.debug){console.log("Run a Queued Function.")}e()}}function c(){this.debug=true}}var queue=new Queue();</script>
<!-- Import CSS -->
<style id="material_css"></style><script>if(typeof window.lsLoadCSSMaxNums === "undefined")window.lsLoadCSSMaxNums = 0;window.lsLoadCSSMaxNums++;lsloader.load("material_css","/css/material.min.css?Z7a72R1E4SxzBKR/WGctOA==",function(){if(typeof window.lsLoadCSSNums === "undefined")window.lsLoadCSSNums = 0;window.lsLoadCSSNums++;if(window.lsLoadCSSNums == window.lsLoadCSSMaxNums)document.documentElement.style.display="";}, false)</script>
<style id="style_css"></style><script>if(typeof window.lsLoadCSSMaxNums === "undefined")window.lsLoadCSSMaxNums = 0;window.lsLoadCSSMaxNums++;lsloader.load("style_css","/css/style.min.css?MKetZV3cUTfDxvMffaOezg==",function(){if(typeof window.lsLoadCSSNums === "undefined")window.lsLoadCSSNums = 0;window.lsLoadCSSNums++;if(window.lsLoadCSSNums == window.lsLoadCSSMaxNums)document.documentElement.style.display="";}, false)</script>
<!-- Config CSS -->
<!-- Other Styles -->
<style>
body, html {
font-family: Roboto, "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
overflow-x: hidden !important;
}
code {
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
}
a {
color: #00838F;
}
.mdl-card__media,
#search-label,
#search-form-label:after,
#scheme-Paradox .hot_tags-count,
#scheme-Paradox .sidebar_archives-count,
#scheme-Paradox .sidebar-colored .sidebar-header,
#scheme-Paradox .sidebar-colored .sidebar-badge{
background-color: #0097A7 !important;
}
/* Sidebar User Drop Down Menu Text Color */
#scheme-Paradox .sidebar-colored .sidebar-nav>.dropdown>.dropdown-menu>li>a:hover,
#scheme-Paradox .sidebar-colored .sidebar-nav>.dropdown>.dropdown-menu>li>a:focus {
color: #0097A7 !important;
}
#post_entry-right-info,
.sidebar-colored .sidebar-nav li:hover > a,
.sidebar-colored .sidebar-nav li:hover > a i,
.sidebar-colored .sidebar-nav li > a:hover,
.sidebar-colored .sidebar-nav li > a:hover i,
.sidebar-colored .sidebar-nav li > a:focus i,
.sidebar-colored .sidebar-nav > .open > a,
.sidebar-colored .sidebar-nav > .open > a:hover,
.sidebar-colored .sidebar-nav > .open > a:focus,
#ds-reset #ds-ctx .ds-ctx-entry .ds-ctx-head a {
color: #0097A7 !important;
}
.toTop {
background: #757575 !important;
}
.material-layout .material-post>.material-nav,
.material-layout .material-index>.material-nav,
.material-nav a {
color: #757575;
}
#scheme-Paradox .MD-burger-layer {
background-color: #757575;
}
#scheme-Paradox #post-toc-trigger-btn {
color: #757575;
}
.post-toc a:hover {
color: #00838F;
text-decoration: underline;
}
</style>
<!-- Theme Background Related-->
<style>
body{
background-color: #fff;
}
/* blog_info bottom background */
#scheme-Paradox .material-layout .something-else .mdl-card__supporting-text{
background-color: #fff;
}
</style>
<!-- Fade Effect -->
<style>
.fade {
transition: all 800ms linear;
-webkit-transform: translate3d(0,0,0);
-moz-transform: translate3d(0,0,0);
-ms-transform: translate3d(0,0,0);
-o-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
opacity: 1;
}
.fade.out{
opacity: 0;
}
</style>
<!-- Import Font -->
<!-- Import Roboto -->
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">
<!-- Import Material Icons -->
<style id="material_icons"></style><script>if(typeof window.lsLoadCSSMaxNums === "undefined")window.lsLoadCSSMaxNums = 0;window.lsLoadCSSMaxNums++;lsloader.load("material_icons","/css/material-icons.css?pqhB/Rd/ab0H2+kZp0RDmw==",function(){if(typeof window.lsLoadCSSNums === "undefined")window.lsLoadCSSNums = 0;window.lsLoadCSSNums++;if(window.lsLoadCSSNums == window.lsLoadCSSMaxNums)document.documentElement.style.display="";}, false)</script>
<!-- Import jQuery -->
<script>lsloader.load("jq_js","/js/jquery.min.js?qcusAULNeBksqffqUM2+Ig==", true)</script>
<!-- WebAPP Icons -->
<meta name="mobile-web-app-capable" content="yes">
<meta name="application-name" content="黄明照--一个在路上慢慢行走的前端人">
<meta name="msapplication-starturl" content="http://www.huangmingzhao.cn/Mongoose.html">
<meta name="msapplication-navbutton-color" content="#0097A7">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="黄明照--一个在路上慢慢行走的前端人">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon" href="/img/favicon.png">
<!-- Site Verification -->
<!-- RSS -->
<!-- The Open Graph protocol -->
<meta property="og:url" content="http://www.huangmingzhao.cn/Mongoose.html">
<meta property="og:type" content="blog">
<meta property="og:title" content="Mongoose | 黄明照--一个在路上慢慢行走的前端人">
<meta property="og:image" content="/img/favicon.png">
<meta property="og:description" content="黄明照、黄明照的个人博客、黄明照的个人网站、一个在路上慢慢行走的前端人">
<meta property="og:article:tag" content="Mongoose">
<meta property="article:published_time" content="Mon Oct 08 2018 22:49:01 GMT+0800">
<meta property="article:modified_time" content="Mon Oct 08 2018 23:44:38 GMT+0800">
<!-- The Twitter Card protocol -->
<meta name="twitter:card" content="summary_large_image">
<!-- Add canonical link for SEO -->
<link rel="canonical" href="http://www.huangmingzhao.cn/Mongoose.html" />
<!-- Structured-data for SEO -->
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "BlogPosting",
"mainEntityOfPage": "http://www.huangmingzhao.cn/Mongoose.html",
"headline": "Mongoose",
"datePublished": "Mon Oct 08 2018 22:49:01 GMT+0800",
"dateModified": "Mon Oct 08 2018 23:44:38 GMT+0800",
"author": {
"@type": "Person",
"name": "黄明照",
"image": {
"@type": "ImageObject",
"url": "/img/avatar1.jpg"
},
"description": "Be A Hero To Myself"
},
"publisher": {
"@type": "Organization",
"name": "黄明照--一个在路上慢慢行走的前端人",
"logo": {
"@type":"ImageObject",
"url": "/img/favicon.png"
}
},
"keywords": ",Mongoose黄明照、黄明照的个人博客、黄明照的个人网站、一个在路上慢慢行走的前端人",
"description": "黄明照、黄明照的个人博客、黄明照的个人网站、一个在路上慢慢行走的前端人",
}
</script>
<!-- Analytics -->
<script>
var _hmt = _hmt || [];
(function() {var hm = document.createElement('script');
hm.src = 'https://hm.baidu.com/hm.js?16de35e801aa6d41f4001b2445157db7';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
<!-- Custom Head -->
</head>
<body id="scheme-Paradox" class="lazy">
<div class="material-layout mdl-js-layout has-drawer is-upgraded">
<!-- Main Container -->
<main class="material-layout__content" id="main">
<!-- Top Anchor -->
<div id="top"></div>
<!-- Hamburger Button -->
<button class="MD-burger-icon sidebar-toggle">
<span class="MD-burger-layer"></span>
</button>
<!-- Post TOC -->
<!-- Back Button -->
<!--
<div class="material-back" id="backhome-div" tabindex="0">
<a class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon"
href="#" onclick="window.history.back();return false;"
target="_self"
role="button"
data-upgraded=",MaterialButton,MaterialRipple">
<i class="material-icons" role="presentation">arrow_back</i>
<span class="mdl-button__ripple-container">
<span class="mdl-ripple"></span>
</span>
</a>
</div>
-->
<!-- Left aligned menu below button -->
<button id="post-toc-trigger-btn"
class="mdl-button mdl-js-button mdl-button--icon">
<i class="material-icons">format_list_numbered</i>
</button>
<ul class="post-toc-wrap mdl-menu mdl-menu--bottom-left mdl-js-menu mdl-js-ripple-effect" for="post-toc-trigger-btn" style="max-height:80vh; overflow-y:scroll;">
<ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#MongoDB"><span class="post-toc-number">1.</span> <span class="post-toc-text">MongoDB</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#何为MongoDB?"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">何为MongoDB?</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#下载安装"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">下载安装</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#概念"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">概念</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#常见使用"><span class="post-toc-number">1.4.</span> <span class="post-toc-text">常见使用</span></a></li></ol></li><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#Mongoose"><span class="post-toc-number">2.</span> <span class="post-toc-text">Mongoose</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#何为Mongoose?"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">何为Mongoose?</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Schema、Model、Entity"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">Schema、Model、Entity</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Schema"><span class="post-toc-number">2.2.1.</span> <span class="post-toc-text">Schema</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Model"><span class="post-toc-number">2.2.2.</span> <span class="post-toc-text">Model</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#Entity"><span class="post-toc-number">2.2.3.</span> <span class="post-toc-text">Entity</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#CRUD"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">CRUD</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#索引"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">索引</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#验证器Validate"><span class="post-toc-number">2.5.</span> <span class="post-toc-text">验证器Validate</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#内置验证器"><span class="post-toc-number">2.5.1.</span> <span class="post-toc-text">内置验证器</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#自定义验证器"><span class="post-toc-number">2.5.2.</span> <span class="post-toc-text">自定义验证器</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#错误提示"><span class="post-toc-number">2.5.3.</span> <span class="post-toc-text">错误提示</span></a></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#联表查询"><span class="post-toc-number">2.6.</span> <span class="post-toc-text">联表查询</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#虚拟属性VirtualType"><span class="post-toc-number">2.7.</span> <span class="post-toc-text">虚拟属性VirtualType</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#插件"><span class="post-toc-number">2.8.</span> <span class="post-toc-text">插件</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#聚合函数"><span class="post-toc-number">2.9.</span> <span class="post-toc-text">聚合函数</span></a></li></ol></li></ol>
</ul>
<!-- Layouts -->
<!-- Post Module -->
<div class="material-post_container">
<div class="material-post mdl-grid">
<div class="mdl-card mdl-shadow--4dp mdl-cell mdl-cell--12-col">
<!-- Post Header(Thumbnail & Title) -->
<!-- Paradox Post Header -->
<!-- Random Thumbnail -->
<div class="post_thumbnail-random mdl-card__media mdl-color-text--grey-50">
<script type="text/ls-javascript" id="post-thumbnail-script">
var randomNum = Math.floor(Math.random() * 19 + 1);
$('.post_thumbnail-random').attr('data-original', '/img/random/material-' + randomNum + '.png');
$('.post_thumbnail-random').addClass('lazy');
</script>
<p class="article-headline-p">
Mongoose
</p>
</div>
<!-- Paradox Post Info -->
<div class="mdl-color-text--grey-700 mdl-card__supporting-text meta">
<!-- Author Avatar -->
<div id="author-avatar">
<img src="/img/avatar1.jpg" width="44px" height="44px" alt="Author Avatar"/>
</div>
<!-- Author Name & Date -->
<div>
<strong>黄明照</strong>
<span>10月 08, 2018</span>
</div>
<div class="section-spacer"></div>
<!-- Favorite -->
<!--
<button id="article-functions-like-button" class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon btn-like">
<i class="material-icons" role="presentation">favorite</i>
<span class="visuallyhidden">favorites</span>
</button>
-->
<!-- Qrcode -->
<!-- Tags (bookmark) -->
<button id="article-functions-viewtags-button" class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon">
<i class="material-icons" role="presentation">bookmark</i>
<span class="visuallyhidden">bookmark</span>
</button>
<ul class="mdl-menu mdl-menu--bottom-right mdl-js-menu mdl-js-ripple-effect" for="article-functions-viewtags-button">
<li class="mdl-menu__item">
<a class="post_tag-link" href="/tags/Mongoose/">Mongoose</a>
</ul>
<!-- Share -->
<button id="article-fuctions-share-button" class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon">
<i class="material-icons" role="presentation">share</i>
<span class="visuallyhidden">share</span>
</button>
<ul class="mdl-menu mdl-menu--bottom-right mdl-js-menu mdl-js-ripple-effect" for="article-fuctions-share-button">
<!-- Leancloud Views -->
<a class="post_share-link" href="#">
<li class="mdl-menu__item">
<span id="/Mongoose.html" class="leancloud-views_num" data-flag-title="Mongoose">
浏览量
</span>
</li>
</a>
<!-- Share Weibo -->
<a class="post_share-link" href="http://service.weibo.com/share/share.php?appkey=&title=Mongoose&url=http://www.huangmingzhao.cn/Mongoose.html&pic=http://www.huangmingzhao.cn/img/favicon.png&searchPic=false&style=simple" target="_blank">
<li class="mdl-menu__item">
分享到微博
</li>
</a>
<!-- Share Twitter -->
<a class="post_share-link" href="https://twitter.com/intent/tweet?text=Mongoose&url=http://www.huangmingzhao.cn/Mongoose.html&via=黄明照" target="_blank">
<li class="mdl-menu__item">
分享到 Twitter
</li>
</a>
<!-- Share Facebook -->
<a class="post_share-link" href="https://www.facebook.com/sharer/sharer.php?u=http://www.huangmingzhao.cn/Mongoose.html" target="_blank">
<li class="mdl-menu__item">
分享到 Facebook
</li>
</a>
<!-- Share Google+ -->
<a class="post_share-link" href="https://plus.google.com/share?url=http://www.huangmingzhao.cn/Mongoose.html" target="_blank">
<li class="mdl-menu__item">
分享到 Google+
</li>
</a>
<!-- Share LinkedIn -->
<!-- Share QQ -->
<a class="post_share-link" href="http://connect.qq.com/widget/shareqq/index.html?site=黄明照--一个在路上慢慢行走的前端人&title=Mongoose&summary=黄明照、黄明照的个人博客、黄明照的个人网站、一个在路上慢慢行走的前端人&pics=http://www.huangmingzhao.cn/img/favicon.png&url=http://www.huangmingzhao.cn/Mongoose.html" target="_blank">
<li class="mdl-menu__item">
分享到 QQ
</li>
</a>
<!-- Share Telegram -->
</ul>
</div>
<!-- Post Content -->
<div id="post-content" class="mdl-color-text--grey-700 mdl-card__supporting-text fade out">
<h1 id="MongoDB"><a href="#MongoDB" class="headerlink" title="MongoDB"></a>MongoDB</h1><h2 id="何为MongoDB?"><a href="#何为MongoDB?" class="headerlink" title="何为MongoDB?"></a>何为MongoDB?</h2><p>MongoDB是一个介于关系型数据库和非关系型数据库之间的开源产品,它是功能最为丰富的非关系型数据库,也是最像关系型数据库的非关系型数据库。</p>
<p>MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。字段值可以包含其他文档,数组及文档数组。</p>
<h2 id="下载安装"><a href="#下载安装" class="headerlink" title="下载安装"></a>下载安装</h2><p>下载:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo curl -O https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.0.tgz</span><br></pre></td></tr></table></figure></p>
<p>解压:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo tar -zxvf mongodb-osx-ssl-x86_64-4.0.0.tgz</span><br></pre></td></tr></table></figure></p>
<p>重命名为mongodb目录:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo mv mongodb-osx-ssl-x86_64-4.0.0 mongodb</span><br></pre></td></tr></table></figure></p>
<h2 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h2><p><img src="http://oubl6fzsm.bkt.clouddn.com/1538979571423.jpg" alt="image"></p>
<h2 id="常见使用"><a href="#常见使用" class="headerlink" title="常见使用"></a>常见使用</h2><ul>
<li><p><strong>查询所有结果</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select * from article</span><br><span class="line"></span><br><span class="line">db.article.find()</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>指定返回哪些键</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select title, author from article</span><br><span class="line"></span><br><span class="line">db.article.find({}, {"title": 1, "author": 1})</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>where条件</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select * from article where title = "深入浅出Node"</span><br><span class="line"></span><br><span class="line">db.article.find({"title": "深入浅出Node"})</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>and条件</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select * from article where title = "深入浅出Node" and author = "朴灵"</span><br><span class="line"></span><br><span class="line">db.article.find({"title": "深入浅出Node", "author": "朴灵"})</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>or条件</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select * from article where title = "深入浅出Node" or author = "朴灵"</span><br><span class="line"></span><br><span class="line">db.article.find({"$or": [{"title": "深入浅出Node"}, {"author": "朴灵"}]})</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>比较条件</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">select * from article where read >= 100</span><br><span class="line"></span><br><span class="line">db.article.find({"like": {"$gt": 100}}) //$gt(>)、$gte(>=)、$lt(<)、$lte(<=) $ne(!=)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">select * from article where read >= 100 and read <= 200</span><br><span class="line"></span><br><span class="line">db.article.find({"like": {"$gte": 100, "lte": 200}})</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>in条件</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select * from article where author in ("a", "b", "c")</span><br><span class="line"></span><br><span class="line">db.article.find({"author": {"$in": ["a", "b", "c"]}})</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>like条件</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select * from article where title like "%深入%"</span><br><span class="line"></span><br><span class="line">db.article.find({"title": /深入/i})</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>取反</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.article.find({"author": {"$not": /mongodb/i}})</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>排序</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">//升序</span><br><span class="line">select * from article where type = "mongodb" order by read desc</span><br><span class="line">db.article.find({"type": "mongodb"}).sort({"like": -1})</span><br><span class="line"></span><br><span class="line">//降序</span><br><span class="line">select * from article where type = "mongodb" order by read asc</span><br><span class="line">db.article.find({"type": "mongodb"}).sort({"like": 1})</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>更新特定字段($set)</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">update article set like = 10000 where _id = 123</span><br><span class="line"></span><br><span class="line">db.article.update({"_id": 123}, { "$set": {"like": 10000}})</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>递增或递减($inc)</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.article.update({"_id": 123}, { "$inc": {"like": 10}}) // 每次count都加10</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>数组追加($push)</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.game.update({"_id": 123}, { "$push": {"comment": '真好'}})</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>删除指定文档</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">delete from article where title = "mongodb"</span><br><span class="line"></span><br><span class="line">db.article.remove({title: "mongodb"})</span><br></pre></td></tr></table></figure>
</li>
<li><p>**limit和skip</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">select * from article limit 10, 20</span><br><span class="line"></span><br><span class="line">db.article.find().skip(10).limit(20) //结合limit()和skip()来达到分页效果</span><br></pre></td></tr></table></figure>
</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">//数据库</span><br><span class="line">use xxx //创建或选择数据库</span><br><span class="line"></span><br><span class="line">show dbs / collections</span><br><span class="line"></span><br><span class="line">use xxx db.dropDatabase() //删除数据库</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">//集合</span><br><span class="line">db.createCollection("mycol", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } ) //创建集合</span><br><span class="line">db.mycol2.insert({"name" : "黄明照"}) //创建集合并插入</span><br><span class="line"></span><br><span class="line">db.mycol2.drop() //删除集合</span><br><span class="line"></span><br><span class="line">db.mycol2.save(...) //无_id插入文档 有_id表示更新</span><br><span class="line"> </span><br><span class="line">db.mycol2.update({'age': 18},{$set:{'age': 20}}) //更新集合</span><br><span class="line"></span><br><span class="line">db.mycol2.remove({'age': 20}) //删除文档</span><br><span class="line"></span><br><span class="line">db.mycol2.find({key1:value1, key2:value2}).pretty() //查询 并and</span><br><span class="line">db.mycol2.find({$or: [{key1: value1}, {key2:value2}]}).pretty() //or 或</span><br><span class="line">db.mycol2.find({"age": {$gt:18}, $or: [{"name": "hmz"},{"name": "黄明照"}]}).pretty() //查询 并和或一起使用</span><br><span class="line"></span><br><span class="line">//limit 和 skip 方法 做分页查询</span><br><span class="line">db.COLLECTION_NAME.find().limit(NUMBER)</span><br><span class="line">db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)</span><br><span class="line"></span><br><span class="line">// 排序</span><br><span class="line">db.col.find({},{"name":1, _id:0}).sort({"age":-1})</span><br></pre></td></tr></table></figure>
<h1 id="Mongoose"><a href="#Mongoose" class="headerlink" title="Mongoose"></a>Mongoose</h1><h2 id="何为Mongoose?"><a href="#何为Mongoose?" class="headerlink" title="何为Mongoose?"></a>何为Mongoose?</h2><p>简单的说,Mongoose就是对node环境中MongoDB数据库操作的封装,一个对象模型工具,将数据库中的数据转换为JavaScript对象以供我们在应用中使用。</p>
<h2 id="Schema、Model、Entity"><a href="#Schema、Model、Entity" class="headerlink" title="Schema、Model、Entity"></a>Schema、Model、Entity</h2><p>在使用Mongoose前,先了解一下 Mongoose 中的三个概念:Schema、Model、Entity</p>
<h3 id="Schema"><a href="#Schema" class="headerlink" title="Schema"></a>Schema</h3><p><code>Schema</code>是一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力,其实也可以看作是表结构的定义。</p>
<p>如何创建一个Schema?<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">const UserSchema = new Schema({</span><br><span class="line"> token: String,</span><br><span class="line"> is_banned: {type: Boolean, default: false}, //是否禁言</span><br><span class="line"> enable: { type: Boolean, default: true }, //用户是否有效</span><br><span class="line"> is_actived: {type: Boolean, default: false}, //邮件激活</span><br><span class="line"> username: String,</span><br><span class="line"> password: String,</span><br><span class="line"> email: String, //email唯一性</span><br><span class="line"> code: String,</span><br><span class="line"> email_time: {type: Date},</span><br><span class="line"> phone: {type: String},</span><br><span class="line"> description: { type: String, default: "这个人很懒,什么都没有留下..." },</span><br><span class="line"> avatar: { type: String, default: "http://p89inamdb.bkt.clouddn.com/default_avatar.png" },</span><br><span class="line"> bg_url: { type: String, default: "http://p89inamdb.bkt.clouddn.com/FkagpurBWZjB98lDrpSrCL8zeaTU"},</span><br><span class="line"> ip: String,</span><br><span class="line"> ip_location: { type: Object },</span><br><span class="line"> agent: { type: String }, // 用户ua</span><br><span class="line"> last_login_time: { type: Date },</span><br><span class="line"> openid: {</span><br><span class="line"> WeiChat: String,</span><br><span class="line"> WeiBo: String,</span><br><span class="line"> QQ: String,</span><br><span class="line"> },</span><br><span class="line"> create_time: { type: Date },</span><br><span class="line"> // retrieve_time: { type: Number }, // 用户发送激活请求的时间</span><br><span class="line"> image_article: [{ type: mongoose.Schema.Types.ObjectId, ref: 'ImageArticle', }],</span><br><span class="line"> collection_image_article: [{ type: mongoose.Schema.Types.ObjectId, ref: 'ImageArticle', }],</span><br><span class="line"> collection_reading_article: [{ type: mongoose.Schema.Types.ObjectId, ref: 'ReadingArticle', }],</span><br><span class="line"> collection_music_article: [{ type: mongoose.Schema.Types.ObjectId, ref: 'MusicArticle', }],</span><br><span class="line"> collection_film_article: [{ type: mongoose.Schema.Types.ObjectId, ref: 'FilmArticle', }],</span><br><span class="line"> collection_sound_article: [{ type: mongoose.Schema.Types.ObjectId, ref: 'SoundArticle', }],</span><br><span class="line"> following_user: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}], //关注</span><br><span class="line"> follower_user: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}] //粉丝</span><br><span class="line">});</span><br></pre></td></tr></table></figure></p>
<h3 id="Model"><a href="#Model" class="headerlink" title="Model"></a>Model</h3><p>由Schema发布生成的模型,具有抽象属性和行为的数据库操作对象。正是Model的存在,让我们操作数据库更加方便快捷。<br>依赖<code>Schema</code>生成一个<code>Model</code>:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">const User = mongoose.model("User", UserSchema);</span><br><span class="line">module.exports = User;</span><br></pre></td></tr></table></figure>
<h3 id="Entity"><a href="#Entity" class="headerlink" title="Entity"></a>Entity</h3><p>由Model创建的实体,它的操作也会影响数据库。<br>依赖<code>Model</code>,创造一个<code>Entity</code>:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">const UserModel = require('../../models/user/user')</span><br><span class="line">const newUser = new UserModel({</span><br><span class="line"> username: '就这样子吧',</span><br><span class="line"> password: sha1('xxx'),</span><br><span class="line"> token: createToken('就这样子吧'),</span><br><span class="line"> email: '651734877@qq.com',</span><br><span class="line"> phone: '13003919397',</span><br><span class="line"> agent: req.headers['user-agent'],</span><br><span class="line"> ip: ip,</span><br><span class="line"> ip_location: geoip.lookup(ip)</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<h2 id="CRUD"><a href="#CRUD" class="headerlink" title="CRUD"></a>CRUD</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line">exports.get = (req, res) => { </span><br><span class="line"> const articleId = req.query['id']; </span><br><span class="line"> ArticlesModel.findById(articleId, (err, result) => { </span><br><span class="line"> if (err) { </span><br><span class="line"> return res.status(400).send({ </span><br><span class="line"> message: '查找失败', data: [] </span><br><span class="line"> }); </span><br><span class="line"> } else { </span><br><span class="line"> res.jsonp({ </span><br><span class="line"> data: [result] </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><br><span class="line"></span><br><span class="line"></span><br><span class="line">exports.add = (req, res) => { </span><br><span class="line"> const article = new ArticlesModel(req.body); </span><br><span class="line"> article.save((err) => { </span><br><span class="line"> if (err) { </span><br><span class="line"> return res.status(400).send({ </span><br><span class="line"> message: '新增失败', </span><br><span class="line"> data: [] </span><br><span class="line"> }); </span><br><span class="line"> } else { </span><br><span class="line"> res.send({ </span><br><span class="line"> data: [article] </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><br><span class="line"></span><br><span class="line">exports.remove = (req, res) => { </span><br><span class="line"> const id = req.query['id']; </span><br><span class="line"></span><br><span class="line"> ArticlesModel.remove({'_id': id}, (err) => { </span><br><span class="line"> if (err) { </span><br><span class="line"> return res.status(400).send({ </span><br><span class="line"> message: '删除失败', </span><br><span class="line"> data: [] </span><br><span class="line"> }); </span><br><span class="line"> } else { </span><br><span class="line"> res.send({status: 1}); </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><br><span class="line">exports.update = (req, res) => { </span><br><span class="line"> const id = req.body['id']; </span><br><span class="line"> ArticlesModel.findById(id, (err, result) => { </span><br><span class="line"> if (err) { </span><br><span class="line"> return res.send({ </span><br><span class="line"> message: '更新失败', </span><br><span class="line"> data: [] </span><br><span class="line"> }); </span><br><span class="line"> } else { </span><br><span class="line"> delete req.body['id']; </span><br><span class="line"> const articles = _.extend(result, req.body);</span><br><span class="line"> articles.save((err, result) => { </span><br><span class="line"> if (err) { </span><br><span class="line"> return res.status(400).send({ </span><br><span class="line"> message: '更新失败', </span><br><span class="line"> data: [] </span><br><span class="line"> }); </span><br><span class="line"> } else { </span><br><span class="line"> res.jsonp({ data: [articles] }); </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><br></pre></td></tr></table></figure>
<h2 id="索引"><a href="#索引" class="headerlink" title="索引"></a>索引</h2><p>索引可以加快查询速度,我们通过一个例子来看看效果。<br>在mongo Shell中,我们创建10000条数据:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ mongo</span><br><span class="line"></span><br><span class="line">for (var i = 0; i < 10000; i++){</span><br><span class="line"> db.users.insert({'name':'user' + i}); </span><br><span class="line">}</span><br></pre></td></tr></table></figure></p>
<p><img src="http://oubl6fzsm.bkt.clouddn.com/mongoose-explain.jpg" alt="image"><br>图中可以看出<code>executionTimeMillis</code>字段为7, 表示执行时间为7毫秒。<br>现在我们来添加索引后执行查询:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">db.hello.ensureIndex({name: 1});</span><br><span class="line">db.hello.find({'name': 'user1000'}).explain('executionStats')</span><br></pre></td></tr></table></figure></p>
<p><img src="http://oubl6fzsm.bkt.clouddn.com/mongoose-explain2.jpg" alt="image"><br>图中可以看出<code>executionTimeMillis</code>字段为1。</p>
<p>从上面的实例可以看出,加了索引后,能快速的查询一条,这可以看到索引极大的提升了查询速度。</p>
<p>下面我们看看如何使用Mongoose创建:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">const ArticlesSchema = new Schema({ </span><br><span class="line"> username: { </span><br><span class="line"> ...</span><br><span class="line"> index: true </span><br><span class="line"> } </span><br><span class="line">});</span><br></pre></td></tr></table></figure></p>
<p><strong>注</strong>: 对于添加的每一条索引,每次写操作(插入、更新、删除)都将耗费更多的时间。这是因为,当数据发生变化时,不仅要更新文档,还要更新集合上的所有索引。因此,mongodb限制每个集合最多有64个索引。通常,在一个特定的集合上,不应该拥有两个以上的索引。</p>
<h2 id="验证器Validate"><a href="#验证器Validate" class="headerlink" title="验证器Validate"></a>验证器Validate</h2><h3 id="内置验证器"><a href="#内置验证器" class="headerlink" title="内置验证器"></a>内置验证器</h3><p>Mongoose提供了几个内置验证器。</p>
<ul>
<li>所有的SchemaType都有内置的require验证器。</li>
<li>数值( Numbers )有最大(man)和最小(min)的验证器。</li>
<li>字符串(String)有enum,maxLength和minLength验证器。</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">const UsersSchema = new Schema({ </span><br><span class="line"> ...</span><br><span class="line"> age: { </span><br><span class="line"> type: Number, </span><br><span class="line"> min: [18, "自定义错误提示"] </span><br><span class="line"> max: 30, </span><br><span class="line"> required: true </span><br><span class="line"> }, </span><br><span class="line"> sex: { </span><br><span class="line"> type: String, </span><br><span class="line"> enum: { </span><br><span class="line"> values: ['male', 'female'],</span><br><span class="line"> message: '`{PATH}` 是 `{VALUE}`, 您必须确认您的性别!' </span><br><span class="line"> }, </span><br><span class="line"> required: true </span><br><span class="line"> } </span><br><span class="line">}); </span><br><span class="line"></span><br><span class="line">mongoose.model('users', UsersSchema);</span><br></pre></td></tr></table></figure>
<h3 id="自定义验证器"><a href="#自定义验证器" class="headerlink" title="自定义验证器"></a>自定义验证器</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">const UsersSchema = new Schema({ </span><br><span class="line"> ... </span><br><span class="line"> phone: { </span><br><span class="line"> type: String, </span><br><span class="line"> validate: { </span><br><span class="line"> validator: function (v) {</span><br><span class="line"> return /1[3|5|8]\d{9}/.test(v);</span><br><span class="line"> },</span><br><span class="line"> message: '`{PATH}` 必须是有效的11位手机号码!' </span><br><span class="line"> }, </span><br><span class="line"> required: true </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><br><span class="line">UsersSchema.path('phone').validate(function (v) {</span><br><span class="line"> return /1[3|5|8]\d{9}/.test(v);</span><br><span class="line">}, '`{PATH}` 必须是有效的11位手机号码!');</span><br></pre></td></tr></table></figure>
<h3 id="错误提示"><a href="#错误提示" class="headerlink" title="错误提示"></a>错误提示</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">const user = new UsersModel(req.body);</span><br><span class="line"></span><br><span class="line">user.save((err, result) => {</span><br><span class="line"> if (err) {</span><br><span class="line"> console.error(err.errors['name']['message']);</span><br><span class="line"> return res.status(400)</span><br><span class="line"> .send({</span><br><span class="line"> message: err</span><br><span class="line"> });</span><br><span class="line"> } else {</span><br><span class="line"> res.jsonp(user);</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure>
<h2 id="联表查询"><a href="#联表查询" class="headerlink" title="联表查询"></a>联表查询</h2><p>如果你使用过MySql,肯定用过join,用来联表查询,但Mongoose中并没有join,不过它提供了一种更方便快捷的方法:<code>Population</code>。<br>用简短的话来概括Population的使用:在一个Collection(articles)中定义一个指向另一个Collection(comment)的_id字段的字段(comment)<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">//FilmComment</span><br><span class="line">const filmCommentSchema = new Schema({</span><br><span class="line"> ...</span><br><span class="line"> article_id: {type: mongoose.Schema.Types.ObjectId, ref: 'FilmArticle', required: true},</span><br><span class="line"> ...</span><br><span class="line"> reply_to_id: {type: mongoose.Schema.Types.ObjectId, ref: 'FilmComment'} //ref的值是模型(model)名称 //被回复的评论id</span><br><span class="line"> ...</span><br><span class="line">})</span><br><span class="line">const FilmComment = mongoose.model('FilmComment', filmCommentSchema);</span><br><span class="line">module.exports = FilmComment</span><br><span class="line"></span><br><span class="line">//FilmArticle</span><br><span class="line">const filmArticleSchema = new Schema({</span><br><span class="line"> ...</span><br><span class="line"> comment: [{type: mongoose.Schema.Types.ObjectId, ref: 'FilmComment'}],</span><br><span class="line"> ...</span><br><span class="line">})</span><br><span class="line">let FilmArticle = mongoose.model('FilmArticle', filmArticleSchema);</span><br><span class="line">module.exports = FilmArticle</span><br><span class="line"></span><br><span class="line">const newFilmComment = req.body.reply_to_id ? new FilmCommentModel({</span><br><span class="line"> article_id: req.body.article_id,</span><br><span class="line"> content: content,</span><br><span class="line"> create_time: Date.now(),</span><br><span class="line"> reply_to_id: req.body.reply_to_id</span><br><span class="line">}) : new FilmCommentModel({</span><br><span class="line"> article_id: req.body.article_id,</span><br><span class="line"> content: content,</span><br><span class="line"> create_time: Date.now(),</span><br><span class="line">})</span><br><span class="line"></span><br><span class="line">await FilmCommentModel.create(newFilmComment)</span><br><span class="line">const newFilmArticle = await FilmArticleModel.findByIdAndUpdate(req.body.article_id, {</span><br><span class="line"> $push': {</span><br><span class="line"> comment: newFilmComment._id</span><br><span class="line"> },</span><br><span class="line">}, {new: true}).populate({</span><br><span class="line"> path: 'comment',</span><br><span class="line"> model: 'FilmComment',</span><br><span class="line"> populate: {</span><br><span class="line"> path: 'reply_to_id',</span><br><span class="line"> model: 'FilmComment',</span><br><span class="line"> populate: {</span><br><span class="line"> path: 'user_id',</span><br><span class="line"> select: {</span><br><span class="line"> password: 0, token: 0</span><br><span class="line"> },</span><br><span class="line"> model: 'User'</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure></p>
<h2 id="虚拟属性VirtualType"><a href="#虚拟属性VirtualType" class="headerlink" title="虚拟属性VirtualType"></a>虚拟属性VirtualType</h2><p>虚拟属性并不会存储到MongoDB中,利用它,我们可以格式化和自定义组合属性值。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">const UsersSchema = new Schema({ </span><br><span class="line"> ... </span><br><span class="line"> address: { </span><br><span class="line"> city: {type: String}, </span><br><span class="line"> street: {type: String} </span><br><span class="line"> } </span><br><span class="line">});</span><br><span class="line">const address = UsersSchema.virtual('address.full'); </span><br><span class="line"></span><br><span class="line">address.get(function () { </span><br><span class="line"> return this.address.city + ' ' + this.address.street; </span><br><span class="line">});</span><br></pre></td></tr></table></figure></p>
<h2 id="插件"><a href="#插件" class="headerlink" title="插件"></a>插件</h2><p>我们是可以通过插件形式来拓展Schema的功能。</p>
<p>比如文章,当我们修改文章时,一般都会添加一个最后编辑时间,虽然我们可以每次修改时都手动更新,但是我们可以通过插件来自动更新。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">// plugins/plugins.js </span><br><span class="line">module.exports = { </span><br><span class="line"> lastModified(schema) {</span><br><span class="line"> schema.add({ lastMod: Date })</span><br><span class="line"> schema.pre('save', function (next) { //前置钩子</span><br><span class="line"> this.lastMod = new Date; </span><br><span class="line"> next() </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><br><span class="line">// articles/filmArticles.js</span><br><span class="line"></span><br><span class="line">const plugins = require('../plugins/plugins'); </span><br><span class="line"></span><br><span class="line">filmArticlesSchema.plugin(plugins.lastModified);</span><br></pre></td></tr></table></figure></p>
<h2 id="聚合函数"><a href="#聚合函数" class="headerlink" title="聚合函数"></a>聚合函数</h2><p>MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果。有点类似sql语句中的 count(*)。</p>
</div>
<!-- Post Comments -->
<div id="comment" style='padding:10px;' class="vcomment"></div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="//unpkg.com/valine/dist/Valine.min.js"></script>
<script>
var GUEST_INFO = ['nick','mail','link'];
var guest_info = 'nick,mail,link'.split(',').filter(function(item){
return GUEST_INFO.indexOf(item) > -1
});
var notify = 'false' == true;
var verify = 'false' == true;
new Valine({
el: '.vcomment',
notify: notify,
verify: verify,
appId: "ztvc2Jdss3JEXFT7bmDXiR6u-gzGzoHsz",
appKey: "SaIXu97lk5KQeyTiQ6VmgDLF",
placeholder: "Just go go",
pageSize:'10',
avatar:'identicon',
lang:'zh-cn'
});
</script>
</div>
<!-- Post Prev & Next Nav -->
<nav class="material-nav mdl-color-text--grey-50 mdl-cell mdl-cell--12-col">
<!-- Prev Nav -->
<!-- Section Spacer -->
<div class="section-spacer"></div>
<!-- Next Nav -->
<a href="/聊聊毕业设计系列2.html" id="post_nav-older" class="next-content">
旧篇
<button class="mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon mdl-color--white mdl-color-text--grey-900" role="presentation">
<i class="material-icons">arrow_forward</i>
</button>
</a>
</nav>
</div>
</div>
<!-- Overlay For Active Sidebar -->
<div class="sidebar-overlay"></div>
<!-- Material sidebar -->
<aside id="sidebar" class="sidebar sidebar-colored sidebar-fixed-left" role="navigation">
<div id="sidebar-main">
<!-- Sidebar Header -->
<div class="sidebar-header header-cover" style="background-image: url(/img/sidebar_header.png);">
<!-- Top bar -->
<div class="top-bar"></div>
<!-- Sidebar toggle button -->
<button type="button" class="sidebar-toggle mdl-button mdl-js-button mdl-js-ripple-effect mdl-button--icon" style="display: initial;" data-upgraded=",MaterialButton,MaterialRipple">
<i class="material-icons">clear_all</i>
<span class="mdl-button__ripple-container">
<span class="mdl-ripple">
</span>
</span>
</button>
<!-- Sidebar Avatar -->
<div class="sidebar-image">
<img src="/img/avatar1.jpg" alt="黄明照's avatar">
</div>
<!-- Sidebar Email -->
<a data-toggle="dropdown" class="sidebar-brand" href="#settings-dropdown">
beheroto@gmail.com
<b class="caret"></b>
</a>
</div>
<!-- Sidebar Navigation -->
<ul class="nav sidebar-nav">
<!-- User dropdown -->
<li class="dropdown">
<ul id="settings-dropdown" class="dropdown-menu">
<li>
<a href="mailto: beheroto@gmail.com" target="_blank" title="Email Me">
<i class="material-icons sidebar-material-icons sidebar-indent-left1pc-element">email</i>
Email Me
</a>
</li>
<li>
<a href="https://github.com/ishareme" target="_blank" title="Github">
<i class="material-icons sidebar-material-icons sidebar-indent-left1pc-element">star</i>
Github
</a>
</li>
</ul>
</li>
<!-- Homepage -->
<li id="sidebar-first-li">
<a href="/">
<i class="material-icons sidebar-material-icons">home</i>
主页
</a>
</li>
<!-- Archives -->
<li class="dropdown">
<a href="#" class="ripple-effect dropdown-toggle" data-toggle="dropdown">
<i class="material-icons sidebar-material-icons">inbox</i>
归档
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li>
<a class="sidebar_archives-link" href="/archives/2018/10/">十月 2018<span class="sidebar_archives-count">1</span></a></li><li><a class="sidebar_archives-link" href="/archives/2018/08/">八月 2018<span class="sidebar_archives-count">2</span></a></li><li><a class="sidebar_archives-link" href="/archives/2018/03/">三月 2018<span class="sidebar_archives-count">3</span></a></li><li><a class="sidebar_archives-link" href="/archives/2018/02/">二月 2018<span class="sidebar_archives-count">6</span></a></li><li><a class="sidebar_archives-link" href="/archives/2018/01/">一月 2018<span class="sidebar_archives-count">1</span></a></li><li><a class="sidebar_archives-link" href="/archives/2017/10/">十月 2017<span class="sidebar_archives-count">3</span></a></li><li><a class="sidebar_archives-link" href="/archives/2017/08/">八月 2017<span class="sidebar_archives-count">3</span></a></li><li><a class="sidebar_archives-link" href="/archives/2017/07/">七月 2017<span class="sidebar_archives-count">2</span></a>
</ul>
</li>
<!-- Categories -->
<li class="dropdown">
<a href="#" class="ripple-effect dropdown-toggle" data-toggle="dropdown">
<i class="material-icons sidebar-material-icons">chrome_reader_mode</i>
分类
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li>
<a class="sidebar_archives-link" href="/categories/渣技术/">渣技术<span class="sidebar_archives-count">18</span></a></li><li><a class="sidebar_archives-link" href="/categories/闲言语/">闲言语<span class="sidebar_archives-count">1</span></a>
</ul>
</li>
<!-- Pages -->
<li>
<a href="https://github.com/ishareme" title="Github">
<i class="material-icons sidebar-material-icons">star</i>
Github
</a>
</li>
<li>
<a href="/links" title="Links">
<i class="material-icons sidebar-material-icons">group</i>
Links
</a>
</li>
<li>
<a href="/gallery" title="Gallery">
<i class="material-icons sidebar-material-icons">photo_camera</i>
Gallery
</a>
</li>
<li class="divider"></li>
<li>
<a href="/tags" title="Tags">
<i class="material-icons sidebar-material-icons">bookmark_border</i>
Tags
</a>
</li>
<li>
<a href="/timeline" title="Timeline">
<i class="material-icons sidebar-material-icons">timeline</i>
Timeline
</a>
</li>
<li>
<a href="http://ougxgesj3.bkt.clouddn.com/%E9%BB%84%E6%98%8E%E7%85%A7-%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91%E5%B7%A5%E7%A8%8B%E5%B8%88.pdf" title="About">
<i class="material-icons sidebar-material-icons">person</i>
About
</a>
</li>
<!-- Article Number -->
<li>
<a href="/archives">
文章总数
<span class="sidebar-badge">21</span>
</a>
</li>
</ul>
<!-- Sidebar Footer -->
<!--
I'm glad you use this theme, the development is no so easy, I hope you can keep the copyright, I will thank you so much.
If you still want to delete the copyrights, could you still retain the first one? Which namely "Theme Material"
It will not impact the appearance and can give developers a lot of support :)
很高兴您使用并喜欢该主题,开发不易 十分谢谢与希望您可以保留一下版权声明。
如果您仍然想删除的话 能否只保留第一项呢?即 "Theme Material"
它不会影响美观并可以给开发者很大的支持和动力。 :)
-->
<!-- Sidebar Divider -->
<div class="sidebar-divider"></div>
<!-- Theme Material -->
<!-- Help & Support -->
<!--
-->
<!-- Feedback -->
<!--
-->
<!-- About Theme -->
<!--
-->
</div>
<!-- Sidebar Image -->
</aside>
<!-- Footer Top Button -->
<div id="back-to-top" class="toTop-wrap">
<a href="#top" class="toTop">
<i class="material-icons footer_top-i">expand_less</i>
</a>
</div>
<!--Footer-->
<footer class="mdl-mini-footer" id="bottom">
<!-- Paradox Footer Left Section -->
<div class="mdl-mini-footer--left-section sns-list">
<!-- Twitter -->
<!-- Facebook -->
<!-- Google + -->
<!-- Weibo -->
<a href="https://weibo.com/3018998921/profile?rightmod=1&wvr=6&mod=personinfo" target="_blank">
<button class="mdl-mini-footer--social-btn social-btn footer-sns-weibo">
<span class="visuallyhidden">Weibo</span>
</button><!--
--></a>
<!-- Instagram -->
<!-- Tumblr -->
<!-- Github -->
<a href="https://github.com/ishareme" target="_blank">
<button class="mdl-mini-footer--social-btn social-btn footer-sns-github">
<span class="visuallyhidden">Github</span>
</button><!--
--></a>
<!-- LinkedIn -->
<a href="https://www.linkedin.com/in/%E6%98%8E%E7%85%A7-%E9%BB%84-79042b105/" target="_blank">
<button class="mdl-mini-footer--social-btn social-btn footer-sns-linkedin">
<span class="visuallyhidden">LinkedIn</span>
</button><!--
--></a>
<!-- Zhihu -->
<a href="https://www.zhihu.com/people/ishareme/activities" target="_blank">
<button class="mdl-mini-footer--social-btn social-btn footer-sns-zhihu">
<span class="visuallyhidden">Zhihu</span>
</button><!--
--></a>
<!-- Bilibili -->
<!-- Telegram -->
<!-- V2EX -->
</div>
<!--Copyright-->
<div id="copyright">
Copyright © 2017 -<script type="text/javascript">var fd = new Date();document.write(" " + fd.getFullYear() + " ");</script>黄明照--一个在路上慢慢行走的前端人
</div>
<!-- Paradox Footer Right Section -->
<!--
I am glad you use this theme, the development is no so easy, I hope you can keep the copyright.
It will not impact the appearance and can give developers a lot of support :)
很高兴您使用该主题,开发不易,希望您可以保留一下版权声明。
它不会影响美观并可以给开发者很大的支持。 :)
-->
<div class="mdl-mini-footer--right-section">
<div>
<div class="footer-develop-div">Powered by <a href="https://hexo.io" target="_blank" class="footer-develop-a">Hexo</a></div>
<div class="footer-develop-div">Theme - <a href="https://github.com/viosey/hexo-theme-material" target="_blank" class="footer-develop-a">Material</a></div>
</div>
</div>
</footer>
<!-- Import JS File -->