-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLearn-MongoDB.html
1 lines (1 loc) · 49.7 KB
/
Learn-MongoDB.html
1
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.0"><link rel="apple-touch-icon" sizes="180x180" href="/images/favicon-32x32.png"><link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png"><link rel="mask-icon" href="/images/favicon-32x32.png" color="#222"><link rel="stylesheet" href="/css/main.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/[email protected]/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/nprogress.css" integrity="sha256-no0c5ccDODBwp+9hSmV5VvPpKwHCpbVzXHexIkupM6U=" crossorigin="anonymous"><script src="https://cdn.jsdelivr.net/npm/[email protected]/nprogress.js" integrity="sha256-a5YRB27CcBwBFcT5EF/f3E4vzIqyHrSR878nseNYw64=" crossorigin="anonymous"></script><script class="next-config" data-name="main" type="application/json">{"hostname":"www.wrysmile.cn","root":"/","images":"/images","scheme":"Gemini","version":"8.5.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":true,"color":"#222","save":"manual"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果:${query}","hits_time":"找到 ${hits} 个搜索结果(用时 ${time} 毫秒)","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script><meta name="description" content="介绍 本文主要记录在学习尚硅谷的 MongoDB 课程时的一些笔记 尚硅谷前端学科全套课程请点击这里进行下载,提取码:afyt"><meta property="og:type" content="article"><meta property="og:title" content="学习笔记:MongoDB(尚硅谷)"><meta property="og:url" content="https://www.wrysmile.cn/Learn-MongoDB.html"><meta property="og:site_name" content="Wrysmile 的博客"><meta property="og:description" content="介绍 本文主要记录在学习尚硅谷的 MongoDB 课程时的一些笔记 尚硅谷前端学科全套课程请点击这里进行下载,提取码:afyt"><meta property="og:locale" content="zh_CN"><meta property="article:published_time" content="2021-06-27T02:16:52.000Z"><meta property="article:modified_time" content="2023-01-31T10:23:56.193Z"><meta property="article:author" content="Zhang Yangeng"><meta property="article:tag" content="前端"><meta property="article:tag" content="学习"><meta property="article:tag" content="数据库"><meta name="twitter:card" content="summary"><link rel="canonical" href="https://www.wrysmile.cn/Learn-MongoDB.html"><script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://www.wrysmile.cn/Learn-MongoDB.html","path":"Learn-MongoDB.html","title":"学习笔记:MongoDB(尚硅谷)"}</script><script class="next-config" data-name="calendar" type="application/json">""</script><title>学习笔记:MongoDB(尚硅谷) | Wrysmile 的博客</title><script>!function(e,t,o,c,i,a,n){e.DaoVoiceObject=i,e[i]=e[i]||function(){(e[i].q=e[i].q||[]).push(arguments)},e[i].l=+new Date,a=t.createElement(o),n=t.getElementsByTagName(o)[0],a.async=1,a.src=c,a.charset="utf-8",n.parentNode.insertBefore(a,n)}(window,document,"script",("https:"==document.location.protocol?"https:":"http:")+"//widget.daovoice.io/widget/13e39436.js","daovoice"),daovoice("init",{app_id:"13e39436"}),daovoice("update")</script><noscript><link rel="stylesheet" href="/css/noscript.css"></noscript><link rel="alternate" href="/atom.xml" title="Wrysmile 的博客" type="application/atom+xml"></head><body itemscope itemtype="http://schema.org/WebPage" class="use-motion"><div class="headband"></div><main class="main"><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏" role="button"><span class="toggle-line"></span> <span class="toggle-line"></span> <span class="toggle-line"></span></div></div><div class="site-meta"><a href="/" class="brand" rel="start"><i class="logo-line"></i><h1 class="site-title">Wrysmile 的博客</h1><i class="logo-line"></i></a><p class="site-subtitle" itemprop="description">欢迎来到我的个人小屋</p><img class="custom-logo-image" src="/images/favicon-32x32.png" alt="Wrysmile 的博客"></div><div class="site-nav-right"><div class="toggle popup-trigger"><i class="fa fa-search fa-fw fa-lg"></i></div></div></div><nav class="site-nav"><ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索</a></li></ul></nav><div class="search-pop-overlay"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocapitalize="off" maxlength="80" placeholder="搜索..." spellcheck="false" type="search" class="search-input"></div><span class="popup-btn-close" role="button"><i class="fa fa-times-circle"></i></span></div><div class="search-result-container no-result"><div class="search-result-icon"><i class="fa fa-spinner fa-pulse fa-5x"></i></div></div></div></div></div><div class="toggle sidebar-toggle" role="button"><span class="toggle-line"></span> <span class="toggle-line"></span> <span class="toggle-line"></span></div><aside class="sidebar"><div class="sidebar-inner sidebar-nav-active sidebar-toc-active"><ul class="sidebar-nav"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="sidebar-panel-container"><div class="post-toc-wrap sidebar-panel"><div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-text">介绍</span></a></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-text">一、基础内容</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%88%86%E7%B1%BB"><span class="nav-text">1.分类</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-text">(1).关系型数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E9%9D%9E%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-text">(2).非关系型数据库</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%AE%80%E4%BB%8B"><span class="nav-text">2.简介</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%AE%89%E8%A3%85"><span class="nav-text">3.安装</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E6%8C%87%E4%BB%A4"><span class="nav-text">4.指令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%9F%BA%E6%9C%AC"><span class="nav-text">(1).基本</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-CRUD"><span class="nav-text">(2).CRUD</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E6%B3%A8%E6%84%8F"><span class="nav-text">5.注意</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-text">二、进阶内容</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%96%87%E6%A1%A3%E4%B9%8B%E9%97%B4%E7%9A%84%E8%81%94%E7%B3%BB"><span class="nav-text">1.文档之间的联系</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%8E%92%E5%BA%8F%E5%92%8C%E6%8A%95%E5%BD%B1"><span class="nav-text">2.排序和投影</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E6%8E%92%E5%BA%8F"><span class="nav-text">(1).排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E6%8A%95%E5%BD%B1"><span class="nav-text">(2).投影</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-Mongoose"><span class="nav-text">3.Mongoose</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E7%AE%80%E4%BB%8B"><span class="nav-text">(1).简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E8%BF%9E%E6%8E%A5%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-text">(2).连接数据库</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%EF%BC%81%E7%9B%91%E5%90%AC%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E8%BF%9E%E6%8E%A5%E7%8A%B6%E6%80%81"><span class="nav-text">!监听数据库的连接状态</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E5%88%9B%E5%BB%BASchema"><span class="nav-text">(3).创建Schema</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E5%88%9B%E5%BB%BAModel"><span class="nav-text">(4).创建Model</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-Module%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-text">(5).Module的方法</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-%E6%8F%92%E5%85%A5%E6%96%87%E6%A1%A3"><span class="nav-text">1).插入文档</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-%E6%9F%A5%E8%AF%A2%E6%96%87%E6%A1%A3"><span class="nav-text">2).查询文档</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-%E4%BF%AE%E6%94%B9%E6%96%87%E6%A1%A3"><span class="nav-text">3).修改文档</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4-%E5%88%A0%E9%99%A4%E6%96%87%E6%A1%A3"><span class="nav-text">4).删除文档</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-%E7%BB%9F%E8%AE%A1%E6%96%87%E6%A1%A3"><span class="nav-text">5).统计文档</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-Document%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-text">(6).Document的方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-%E6%A8%A1%E5%9D%97%E5%8C%96"><span class="nav-text">(7).模块化</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-text">三、案例分析</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%90%91%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD%E6%B7%BB%E5%8A%A020000%E6%9D%A1%E6%95%B0%E6%8D%AE"><span class="nav-text">1.向数据库中添加20000条数据</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E6%99%AE%E9%80%9A%E6%96%B9%E6%B3%95"><span class="nav-text">(1).普通方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E4%BC%98%E5%8C%96%E6%96%B9%E6%B3%95"><span class="nav-text">(2).优化方法</span></a></li></ol></li></ol></li></ol></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="Zhang Yangeng" src="/images/avatar.jpg"><p class="site-author-name" itemprop="name">Zhang Yangeng</p><div class="site-description" itemprop="description">保持独立思考,不卑不亢不怂,长成自己想要的样子!</div></div><div class="site-state-wrap site-overview-item animated"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">69</span> <span class="site-state-item-name">日志</span></a></div><div class="site-state-item site-state-categories"><a href="/categories/"><span class="site-state-item-count">13</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/"><span class="site-state-item-count">52</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="links-of-author site-overview-item animated"><span class="links-of-author-item"><a href="https://github.com/zhangyangeng" title="GitHub → https://github.com/zhangyangeng" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a> </span><span class="links-of-author-item"><a href="https://weibo.com/u/2294901673" title="weibo → https://weibo.com/u/2294901673" rel="external nofollow noopener noreferrer" target="_blank"><i class="fab fa-weibo fa-fw"></i>weibo</a></span></div><div class="cc-license site-overview-item animated" itemprop="license"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" class="cc-opacity" rel="external nofollow noopener noreferrer" target="_blank"><img src="https://cdn.jsdelivr.net/npm/@creativecommons/[email protected]/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a></div><div class="links-of-blogroll site-overview-item animated"><div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i> 推荐网站</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"><a href="http://topbook.cc/" title="http://topbook.cc/" rel="external nofollow noopener noreferrer" target="_blank">Topbook</a></li></ul></div></div></div></div></aside><div class="sidebar-dimmer"></div></header><div class="back-to-top" role="button" aria-label="返回顶部"><i class="fa fa-arrow-up"></i> <span>0%</span></div><div class="reading-progress-bar"></div><a role="button" class="book-mark-link book-mark-link-fixed"></a><noscript><div class="noscript-warning">Theme NexT works best with JavaScript enabled</div></noscript><div class="main-inner post posts-expand"><div class="post-block"><article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN"><link itemprop="mainEntityOfPage" href="https://www.wrysmile.cn/Learn-MongoDB.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/images/avatar.jpg"><meta itemprop="name" content="Zhang Yangeng"><meta itemprop="description" content="保持独立思考,不卑不亢不怂,长成自己想要的样子!"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="Wrysmile 的博客"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">学习笔记:MongoDB(尚硅谷)</h1><div class="post-meta-container"><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间:2021-06-27 10:16:52" itemprop="dateCreated datePublished" datetime="2021-06-27T10:16:52+08:00">2021-06-27</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-calendar-check"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间:2023-01-31 18:23:56" itemprop="dateModified" datetime="2023-01-31T18:23:56+08:00">2023-01-31</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="far fa-folder"></i> </span><span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">学习笔记</span></a> </span></span><span id="/Learn-MongoDB.html" class="post-meta-item leancloud_visitors" data-flag-title="学习笔记:MongoDB(尚硅谷)" title="阅读次数"><span class="post-meta-item-icon"><i class="far fa-eye"></i> </span><span class="post-meta-item-text">阅读次数:</span> <span class="leancloud-visitors-count"></span></span></div><div class="post-meta"><span class="post-meta-item" title="本文字数"><span class="post-meta-item-icon"><i class="far fa-file-word"></i> </span><span class="post-meta-item-text">本文字数:</span> <span>5.7k</span> </span><span class="post-meta-item" title="阅读时长"><span class="post-meta-item-icon"><i class="far fa-clock"></i> </span><span class="post-meta-item-text">阅读时长 ≈</span> <span>5 分钟</span></span></div></div></header><div class="post-body" itemprop="articleBody"><h1>介绍</h1><ul class="lvl-0"><li class="lvl-2">本文主要记录在学习尚硅谷的 MongoDB 课程时的一些笔记</li><li class="lvl-2">尚硅谷前端学科全套课程请点击<a target="_blank" rel="external nofollow noopener noreferrer" href="https://pan.baidu.com/s/1BM_OKMXXAGxMNqaBN_7tRg">这里</a>进行下载,提取码:afyt<span id="more"></span></li></ul><h1>一、基础内容</h1><h2 id="1-分类">1.分类</h2><h3 id="1-关系型数据库">(1).关系型数据库</h3><ul class="lvl-0"><li class="lvl-2"><p>关系型数据库(RDBMS)中全都是表</p></li><li class="lvl-2"><p>例如:MySQL、Oracle、DB2、SQL Server</p></li></ul><h3 id="2-非关系型数据库">(2).非关系型数据库</h3><ul class="lvl-0"><li class="lvl-2"><p>非关系型数据库(NoSQL、Not Only SQL)分为好多种类,主要有:</p></li><li class="lvl-2"><p>键值对数据库:Redis</p></li><li class="lvl-2"><p>文档数据库:MongoDB</p></li></ul><h2 id="2-简介">2.简介</h2><ul class="lvl-0"><li class="lvl-2"><p>MongoDB 的数据模型是面向文档的,即类似于 JSON 的结构(又名BSON)</p></li><li class="lvl-2"><p>主要概念如下:</p><ul class="lvl-2"><li class="lvl-4">数据库(database):一个仓库,可以存放集合</li><li class="lvl-4">集合(collection):类似于数组,可以存放文档</li><li class="lvl-4">文档(document):文档数据库中的最小单位,存储和操作的内容都是文档</li></ul></li></ul><h2 id="3-安装">3.安装</h2><ul class="lvl-0"><li class="lvl-2"><p>可以去<a target="_blank" rel="external nofollow noopener noreferrer" href="https://www.mongodb.com/">官网</a>下载最新版,也可以去<a target="_blank" rel="external nofollow noopener noreferrer" href="https://www.mongodb.org/dl/win32/">这里</a>下载,下载社区版即可(3.2版本之后不支持32位系统,且最新版安装无需配置 data 目录,会自动生成)</p></li><li class="lvl-2"><p>配置环境变量:</p><ul class="lvl-2"><li class="lvl-4">打开 MongoDB 安装目录下的 <code>bin</code> 文件夹,将该路径添加到系统的环境变量中</li></ul></li><li class="lvl-2"><p>打开 cmd 窗口,直接输入 <code>mongo</code> 命令即可连接到 MongoDB 数据库</p></li><li class="lvl-2"><p>可通过以下命令更改默认数据库位置和端口号</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mongod --dbpath 数据库路径 --port 端口号 // 端口号一般介于1000-65535之间</span><br></pre></td></tr></table></figure><h2 id="4-指令">4.指令</h2><h3 id="1-基本">(1).基本</h3><ul class="lvl-0"><li class="lvl-2"><p><code>show dbs/datebases</code>:显示当前的所有数据库</p></li><li class="lvl-2"><p><code>use 数据库名</code>:进入到指定的数据库中</p></li><li class="lvl-2"><p><code>db</code>:显示当前所处的数据库</p></li><li class="lvl-2"><p><code>show collections</code>:显示数据库中所有集合</p></li></ul><h3 id="2-CRUD">(2).CRUD</h3><ul class="lvl-0"><li class="lvl-2"><p>插入:</p><ul class="lvl-2"><li class="lvl-4"><code>db.<集合名>.insert(doc)</code>:向集合中插入文档(可一个可多个)</li><li class="lvl-4"><code>db.<集合名>.insertOne(doc)</code>:向集合中插入一个文档</li><li class="lvl-4"><code>db.<集合名>.insertMany(doc)</code>:向集合中插入多个文档</li></ul></li><li class="lvl-2"><p>查询:</p><ul class="lvl-2"><li class="lvl-4"><code>db.<集合名>.find()</code>:查询集合中的所有文档,返回的是一个数组</li><li class="lvl-4"><code>db.<集合名>.find().count()</code>:查询集合中所有文档的数量</li><li class="lvl-4"><code>db.<集合名>.find({属性:值})</code>:查询集合中属性是指定值的文档,若需要查询指定的属性,需要使用“查询操作符”来完成查询<ul class="lvl-4"><li class="lvl-6"><code>$gt</code>:大于指定属性的值</li><li class="lvl-6"><code>$lt</code>:小雨指定属性的值</li></ul></li><li class="lvl-4"><code>db.<集合名>.findOne()</code>:查询集合中符合条件的第一个文档,返回的是一个对象</li><li class="lvl-4"><code>db.<集合名>.find().limit(10)</code>:设置显示数据的上限</li><li class="lvl-4"><code>db.<集合名>.find().skip(10).limit(10)</code>:跳过前10条数据<ul class="lvl-4"><li class="lvl-6">skip((页码 -1 ) * 每页显示的条数)</li><li class="lvl-6">limit(每页显示的条数)</li></ul></li></ul></li><li class="lvl-2"><p>修改:</p><ul class="lvl-2"><li class="lvl-4"><code>db.<集合名>.update({查询条件},{新对象})</code>:默认情况会使用新对象覆盖旧对象,若需要修改指定的属性,需要使用“修改操作符”来完成修改<ul class="lvl-4"><li class="lvl-6"><code>$set</code>:修改文档中的指定属性</li><li class="lvl-6"><code>$unset</code>:删除文档中的指定属性</li><li class="lvl-6"><code>$push/$addToSet</code>:向数组中添加一个新的元素(后者如果数组中已存在该元素,则不会添加)</li><li class="lvl-6"><code>$inc</code>:进行自增操作</li></ul></li><li class="lvl-4"><code>db.<集合名>.updateMany()</code>:同时修改多个符合条件的文档</li><li class="lvl-4"><code>db.<集合名>.updateOne()</code>:修改一个符合条件的文档</li><li class="lvl-4"><code>db.<集合名>.replaceOne()</code>:替换一个文档</li></ul></li><li class="lvl-2"><p>删除:</p><ul class="lvl-2"><li class="lvl-4"><code>db.<集合名>.remove()</code>:删除符合条件的所有文档(默认情况下会删除多个,但第二个参数传true时只会删除一个)</li><li class="lvl-4"><code>db.<集合名>.remove({})</code>:删除集合中的所有文档</li><li class="lvl-4"><code>db.<集合名>.deleteOne()</code>:删除符合条件的一个文档</li><li class="lvl-4"><code>db.<集合名>.deleteMany()</code>:删除符合条件的多个文档</li><li class="lvl-4"><code>db.<集合名>.drop()</code>:删除集合</li><li class="lvl-4"><code>db.dropDatabase()</code>:删除数据库</li><li class="lvl-4"><strong style="color:red">!注意:一般数据库中的数据都不会删除,一般会在数据中添加一个字段,来表示数据是否被删除</strong></li></ul></li></ul><h2 id="5-注意">5.注意</h2><ul class="lvl-0"><li class="lvl-2"><p>在 MongoDB 中当一个文档的属性值是一个文档时,这个文档为内嵌文档</p></li><li class="lvl-2"><p>在 MongoDB 中支持直接通过内嵌文档的属性进行查询,如果要查询内嵌文档则可通过 <code>.</code> 的形式来匹配,<strong style="color:red">但此时的属性名必须使用引号</strong></p></li></ul><h1>二、进阶内容</h1><h2 id="1-文档之间的联系">1.文档之间的联系</h2><ul class="lvl-0"><li class="lvl-2"><p>一对一</p><ul class="lvl-2"><li class="lvl-4">例子:夫妻</li><li class="lvl-4">可以通过内嵌文档的形式来体现出一对一的关系</li></ul></li><li class="lvl-2"><p>一对多</p><ul class="lvl-2"><li class="lvl-4">例子:父母 - 孩子、用户 - 订单、文章 - 评论、</li><li class="lvl-4">可以通过内嵌文档来映射一对多的关系</li></ul></li><li class="lvl-2"><p>多对多</p><ul class="lvl-2"><li class="lvl-4">例子:分类 - 商品、老师 - 学生</li></ul></li></ul><h2 id="2-排序和投影">2.排序和投影</h2><h3 id="1-排序">(1).排序</h3><ul class="lvl-0"><li class="lvl-2"><p>在使用 <code>find()</code> 查询文档时,默认是按照 <code>_id</code> 的值进行升序排序的</p></li><li class="lvl-2"><p>我们可以使用 <code>sort()</code> 来指定文档的排序规则,且需要传递一个对象来指定排序规则</p><ul class="lvl-2"><li class="lvl-4">1 表示升序</li><li class="lvl-4">-1 表示降序</li></ul></li><li class="lvl-2"><p>当在 <code>sort()</code> 中输入第二个参数时,表示先以第一个参数排序,当第一个参数相等时按第二个参数排序</p></li></ul><h3 id="2-投影">(2).投影</h3><ul class="lvl-0"><li class="lvl-2"><p>当我们只想要获取某个文档中的某个字段时可以使用 <strong>投影</strong></p></li><li class="lvl-2"><p>在查询时,可以在第二个参数的位置来设置查询结果的投影</p><ul class="lvl-2"><li class="lvl-4">1 表示显示</li><li class="lvl-4">0 表示不显示</li><li class="lvl-4">默认显示 <code>_id</code></li></ul></li></ul><h2 id="3-Mongoose">3.Mongoose</h2><h3 id="1-简介">(1).简介</h3><ul class="lvl-0"><li class="lvl-2"><p>官网:点击<a target="_blank" rel="external nofollow noopener noreferrer" href="https://mongoosejs.com/">这里</a></p></li><li class="lvl-2"><p>Mongoose 是一个对象文档模型(ODM)库,对 Node 原生的 MongoDB 模块进一步的优化封装并提供了更多的功能</p></li><li class="lvl-2"><p>Mongoose 的好处:</p><ul class="lvl-2"><li class="lvl-4">可以为文档创建一个模式结构(Schema)</li><li class="lvl-4">可以对模型中的对象/文档进行验证</li><li class="lvl-4">数据可以通过类型转换转换为对象模型</li><li class="lvl-4">可以使用中间件来应用业务逻辑挂钩</li><li class="lvl-4">比 Node 原生的 MongoDB 驱动更容易</li></ul></li><li class="lvl-2"><p>Mongoose 新对象:</p><ul class="lvl-2"><li class="lvl-4">Schema(模式对象):约束数据库中的文档结构</li><li class="lvl-4">Model:作为集合中的所有文档的标识,相当于数据库中的集合 Collection</li><li class="lvl-4">Document:表示集合中的具体文档</li></ul></li></ul><h3 id="2-连接数据库">(2).连接数据库</h3><ul class="lvl-0"><li class="lvl-2"><p>在当前项目路径的终端中进行安装</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i mongoose --save</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>在当前项目中引入 Mongoose</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">const mongoose = require("mongoose");</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>连接 MongoDB 数据库</p></li></ul><figure class="highlight plaintext"><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">mongoose.connect("mongodb://数据库的ip地址:端口号/数据库名", {useNewUrlParser: true, useUnifiedTopology: true});</span><br><span class="line">mongoose.connect("mongodb://localhost:27017/mongoose_test");</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>断开 MongoDB 数据库(一般不调用,连接一次后除非项目停止、服务器关闭,否则连接一般不会断开)</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mongoose.disconnect()</span><br></pre></td></tr></table></figure><h4 id="!监听数据库的连接状态">!监听数据库的连接状态</h4><ul class="lvl-0"><li class="lvl-2"><p>在 Mongoose 对象中,有一个属性叫做 connection,该对象表示的就是数据库连接,通过监视该对象的状态就可以监听数据库的连接与断开</p></li><li class="lvl-2"><p>数据库连接成功的事件:</p></li></ul><figure class="highlight plaintext"><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">mongoose.connection.once("open", function (){</span><br><span class="line"> console.log("数据库连接成功");</span><br><span class="line">})</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>数据库连接断开的事件:</p></li></ul><figure class="highlight plaintext"><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">mongoose.connection.once("close", function (){</span><br><span class="line"> console.log("数据库连接断开");</span><br><span class="line">})</span><br></pre></td></tr></table></figure><h3 id="3-创建Schema">(3).创建Schema</h3><ul class="lvl-0"><li class="lvl-2"><p>通过创建 Schema 对象来进行</p></li></ul><figure class="highlight plaintext"><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></pre></td><td class="code"><pre><span class="line">const Schema = mongoose.Schema;</span><br><span class="line">const stuSchema = new Schema({</span><br><span class="line"> name: String,</span><br><span class="line"> age: Number,</span><br><span class="line"> gender: {</span><br><span class="line"> type: String,</span><br><span class="line"> default: "male"</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>如果想要设置默认值,可以通过以下方式进行设置:</p></li></ul><figure class="highlight plaintext"><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></pre></td><td class="code"><pre><span class="line">gender: {</span><br><span class="line"> type: String,</span><br><span class="line"> default: "male"</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="4-创建Model">(4).创建Model</h3><ul class="lvl-0"><li class="lvl-2"><p>可通过以下命令来创建 Model,参数一为要映射的集合名,参数二为创建好的 Schema 对象</p></li><li class="lvl-2"><p>mongoose 会自动将集合名变为复数</p></li></ul><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">const StuModel = mongoose.model("students", stuSchema);</span><br></pre></td></tr></table></figure><h3 id="5-Module的方法">(5).Module的方法</h3><h4 id="1-插入文档">1).插入文档</h4><ul class="lvl-0"><li class="lvl-2"><p>使用以下命令来插入文档,参数一为需要插入的文档内容,参数二为回调函数</p></li></ul><figure class="highlight plaintext"><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">StuModel.create({</span><br><span class="line"> name: "孙悟空",</span><br><span class="line"> age: 18</span><br><span class="line">}, function (err){</span><br><span class="line"> if(!err){</span><br><span class="line"> console.log("插入成功!");</span><br><span class="line"> }</span><br><span class="line">})</span><br></pre></td></tr></table></figure><h4 id="2-查询文档">2).查询文档</h4><ul class="lvl-0"><li class="lvl-2"><p><code>Module.find()</code>:查询所有符合条件的文档</p></li><li class="lvl-2"><p><code>Module.findOne()</code>:查询符合条件的第一个文档</p></li><li class="lvl-2"><p><code>Module.findById()</code>:根据文档id属性查询文档</p></li><li class="lvl-2"><p>参数有:</p><ul class="lvl-2"><li class="lvl-4">condition:查询的条件(对象)</li><li class="lvl-4">projection:投影(可以使用传统方式,也可以传入一个对象 <code>"name age -_id"</code> 来投影)</li><li class="lvl-4">options:查询选项(skip、limit)</li><li class="lvl-4">callback:回调函数,必传否则不会查询</li></ul></li></ul><h4 id="3-修改文档">3).修改文档</h4><ul class="lvl-0"><li class="lvl-2"><p><code>Module.update()</code></p></li><li class="lvl-2"><p><code>Module.updateMany()</code></p></li><li class="lvl-2"><p><code>Module.updateOne()</code></p></li><li class="lvl-2"><p><code>Module.replaceOne()</code></p></li><li class="lvl-2"><p>参数有:</p><ul class="lvl-2"><li class="lvl-4">condition:查询条件</li><li class="lvl-4">doc:修改后的对象</li><li class="lvl-4">options:配置参数</li><li class="lvl-4">callback:回调函数</li></ul></li></ul><h4 id="4-删除文档">4).删除文档</h4><ul class="lvl-0"><li class="lvl-2"><p><code>Module.remove()</code></p></li><li class="lvl-2"><p><code>Module.deleteOne()</code></p></li><li class="lvl-2"><p><code>Module.deleteMany()</code></p></li><li class="lvl-2"><p>参数有:</p><ul class="lvl-2"><li class="lvl-4">conditions:查询条件</li><li class="lvl-4">callback:回调函数</li></ul></li></ul><h4 id="5-统计文档">5).统计文档</h4><ul class="lvl-0"><li class="lvl-2"><p><code>Module.count(conditions, callback)</code>:统计文档的数量</p></li></ul><h3 id="6-Document的方法">(6).Document的方法</h3><ul class="lvl-0"><li class="lvl-2"><p>Document 是 Module 的实例,和集合中的文档一一对应</p></li><li class="lvl-2"><p><code>document.save()</code>:保存到数据库</p></li><li class="lvl-2"><p><code>document.update()</code>:修改对象</p></li><li class="lvl-2"><p><code>document.remove()</code>:删除对象</p></li><li class="lvl-2"><p><code>document.get()</code>:获取文档中的指定属性值</p></li><li class="lvl-2"><p><code>document.set()</code>:设置文档中的指定属性值</p></li><li class="lvl-2"><p><code>document.id()</code>:获取文档的 _id 属性值</p></li><li class="lvl-2"><p><code>document.toJSON()</code>:转换为一个 JSON 对象</p></li><li class="lvl-2"><p><code>document.toObject()</code>:转换为一个 Object 对象,转换之后所有的 Document 对象的方法或属性都不能使用</p></li></ul><h3 id="7-模块化">(7).模块化</h3><ul class="lvl-0"><li class="lvl-2"><p>把连接数据库的代码封装到一个工具文件中,如下:</p></li></ul><figure class="highlight plaintext"><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></pre></td><td class="code"><pre><span class="line"><-- conn_mongodb.js --></span><br><span class="line">const mongoose = require("mongoose");</span><br><span class="line">mongoose.connect("mongodb://127.0.0.1:27017/mongoose_test");</span><br><span class="line">mongoose.connection.once("open", function (){</span><br><span class="line"> console.log("数据库连接成功");</span><br><span class="line">})</span><br><span class="line">mongoose.connection.once("close", function (){</span><br><span class="line"> console.log("数据库连接关闭");</span><br><span class="line">})</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>把创建 Schema 和 Model 的代码封装到专门存放Model对象的文件夹中,如下:</p></li></ul><figure class="highlight plaintext"><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"><-- student.js --></span><br><span class="line">const mongoose = require("mongoose")</span><br><span class="line">const Schema = mongoose.Schema;</span><br><span class="line">const stuSchema = new Schema({</span><br><span class="line"> name: String,</span><br><span class="line"> age: Number,</span><br><span class="line"> gender: {</span><br><span class="line"> type: String,</span><br><span class="line"> default: "male"</span><br><span class="line"> }</span><br><span class="line">})</span><br><span class="line">const StuModel = mongoose.model("student", stuSchema);</span><br><span class="line">module.exports = StuModel;</span><br></pre></td></tr></table></figure><ul class="lvl-0"><li class="lvl-2"><p>最后在所需的文件中直接引入这两个内容即可</p></li></ul><figure class="highlight plaintext"><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"><-- model.js --></span><br><span class="line">require("./tools/conn_mongodb");</span><br><span class="line">const student = require("./modules/student");</span><br><span class="line">student.find({}, function (err, docs){</span><br><span class="line"> console.log(docs);</span><br><span class="line">})</span><br></pre></td></tr></table></figure><h1>三、案例分析</h1><h2 id="1-向数据库中添加20000条数据">1.向数据库中添加20000条数据</h2><h3 id="1-普通方法">(1).普通方法</h3><ul class="lvl-0"><li class="lvl-2"><p>将数据库插入操作放入循环中,<strong style="color:red">性能极差,耗时长</strong></p></li></ul><figure class="highlight plaintext"><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">for(let i = 1; i <= 20000; i++){</span><br><span class="line"> db.numbers.insert({num:i})</span><br><span class="line">}</span><br></pre></td></tr></table></figure><h3 id="2-优化方法">(2).优化方法</h3><ul class="lvl-0"><li class="lvl-2"><p>先循环将20000条数据添加到数组中,再将这个数组插入到数据库的集合中,<strong style="color:red">性能好,耗时短,推荐</strong></p></li></ul><figure class="highlight plaintext"><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">let arr = [];</span><br><span class="line">for(let i = 1; i <= 20000; i++){</span><br><span class="line"> arr.push({num : i})</span><br><span class="line">}</span><br><span class="line">db.numbers.insert(arr);</span><br></pre></td></tr></table></figure></div><footer class="post-footer"><div class="my_post_copyright"><script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script><script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script><script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script><p><span>本文标题:</span><a href="/Learn-MongoDB.html">学习笔记:MongoDB(尚硅谷)</a></p><p><span>文章作者:</span><a href="/" title="访问 Zhang Yangeng 的个人博客">Zhang Yangeng</a></p><p><span>发布时间:</span>2021年06月27日 - 10:06</p><p><span>最后更新:</span>2023年01月31日 - 18:01</p><p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="external nofollow noopener noreferrer" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a></p><p><span>注意事项:</span>转载请保留原文链接及作者,请尊重作者的劳动成果</p><p><span>原始链接:</span><a href="/Learn-MongoDB.html" title="学习笔记:MongoDB(尚硅谷)">https://www.wrysmile.cn/Learn-MongoDB.html</a> <span class="copy-path" title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://www.wrysmile.cn/Learn-MongoDB.html" aria-label="复制成功!"></i></span></p></div><script>var clipboard=new Clipboard(".fa-clipboard");$(".fa-clipboard").click(function(){clipboard.on("success",function(){swal({title:"",text:"复制成功",icon:"success",showConfirmButton:!0})})})</script><div class="reward-container"><div>坚持原创技术分享,您的支持将是对我最大的鼓励!</div><button onclick='document.querySelector(".post-reward").classList.toggle("active")'>赞赏</button><div class="post-reward"><div><img src="https://s2.ax1x.com/2019/01/28/kKrX7T.png" alt="Zhang Yangeng 微信"> <span>微信</span></div><div><img src="https://i.loli.net/2019/01/25/5c4ac75f2e5b7.jpg" alt="Zhang Yangeng 支付宝"> <span>支付宝</span></div></div></div><div class="post-tags"><a href="/tags/%E5%89%8D%E7%AB%AF/" rel="tag"><i class="fa fa-tag"></i> 前端</a> <a href="/tags/%E5%AD%A6%E4%B9%A0/" rel="tag"><i class="fa fa-tag"></i> 学习</a> <a href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/" rel="tag"><i class="fa fa-tag"></i> 数据库</a></div><div class="post-nav"><div class="post-nav-item"><a href="/Learn-NodeJS.html" rel="prev" title="学习笔记:NodeJS(尚硅谷)"><i class="fa fa-chevron-left"></i> 学习笔记:NodeJS(尚硅谷)</a></div><div class="post-nav-item"><a href="/Learn-AJAX.html" rel="next" title="学习笔记:AJAX(尚硅谷)">学习笔记:AJAX(尚硅谷) <i class="fa fa-chevron-right"></i></a></div></div></footer></article></div><div class="comments" id="lv-container" data-id="city" data-uid="MTAyMC80MjQ5Ni8xOTA0Mw=="></div></div></main><footer class="footer"><div class="footer-inner"><div class="copyright">© 2019 – <span itemprop="copyrightYear">2023</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">Wrysmile</span></div><div class="wordcount"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-chart-line"></i> </span><span>站点总字数:</span> <span title="站点总字数">413k</span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-coffee"></i> </span><span>站点阅读时长 ≈</span> <span title="站点阅读时长">6:15</span></span></div><div class="busuanzi-count"><span class="post-meta-item" id="busuanzi_container_site_uv"><span class="post-meta-item-icon"><i class="fa fa-user"></i> </span><span class="site-uv" title="总访客量"><span id="busuanzi_value_site_uv"></span> </span></span><span class="post-meta-item" id="busuanzi_container_site_pv"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="site-pv" title="总访问量"><span id="busuanzi_value_site_pv"></span></span></span></div><script color="0,0,255" opacity="0.5" zindex="-1" count="99" src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1/dist/canvas-nest.js"></script><script type="text/javascript">!function(e,r){function t(){for(var e=0;e<n.length;e++)n[e].alpha<=0?(r.body.removeChild(n[e].el),n.splice(e,1)):(n[e].y--,n[e].scale+=.004,n[e].alpha-=.013,n[e].el.style.cssText="left:"+n[e].x+"px;top:"+n[e].y+"px;opacity:"+n[e].alpha+";transform:scale("+n[e].scale+","+n[e].scale+") rotate(45deg);background:"+n[e].color+";z-index:99999");requestAnimationFrame(t)}var o,n=[];e.requestAnimationFrame=e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)},function(t){var a=r.createElement("style");a.type="text/css";try{a.appendChild(r.createTextNode(t))}catch(e){a.styleSheet.cssText=t}r.getElementsByTagName("head")[0].appendChild(a)}(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o="function"==typeof e.onclick&&e.onclick,e.onclick=function(e){var t,a;o&&o(),t=e,(a=r.createElement("div")).className="heart",n.push({el:a,x:t.clientX-5,y:t.clientY-5,scale:1,alpha:1,color:"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}),r.body.appendChild(a)},t()}(window,document)</script><script type="text/javascript">var titleTime,OriginTitle=document.title;document.addEventListener("visibilitychange",function(){document.hidden?($('[rel="icon"]').attr("href","/img/TEP.ico"),document.title="╭(°A°`)╮ 页面崩溃啦 ~",clearTimeout(titleTime)):($('[rel="icon"]').attr("href","/favicon.ico"),document.title="(ฅ>ω<*ฅ) 噫又好了~"+OriginTitle,titleTime=setTimeout(function(){document.title=OriginTitle},2e3))})</script></div></footer><script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/[email protected]/dist/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script><script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/third-party/search/local-search.js"></script><script class="next-config" data-name="nprogress" type="application/json">{"enable":true,"spinner":false}</script><script src="/js/third-party/nprogress.js"></script><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><script class="next-config" data-name="leancloud_visitors" type="application/json">{"enable":true,"app_id":"mzhbgeWR9BA3SvqtvaAJulC1-gzGzoHsz","app_key":"xcGndtx2gmyop5yrT6euUk9G","server_url":null,"security":false}</script><script src="/js/third-party/statistics/lean-analytics.js"></script><script src="/js/third-party/comments/livere.js"></script></body></html>