大熊君大话NodeJS之------MongoDB模块(额外篇)
<p>一,开篇分析</p><p>这篇属于扩展知识篇,因为在下面的文章中会用到数据库操作,所以今天就来说说它(<span style="color: rgba(255, 0, 0, 1)"><strong>Mongodb模块</strong></span>)。</p>
<p><span style="font-family: 宋体; font-size: 14px; color: rgba(255, 0, 0, 1)"><strong>(1),简介</strong></span></p>
<p><span style="font-family: 宋体; font-size: 14px"> MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。</span></p>
<p><span style="font-family: 宋体; font-size: 14px">传统的关系数据库一般由数据库(database)、表(table)、记录(record)三个层次概念组成,MongoDB是由数据库(database)、集合(collection)、文档对象(document)三个层次组成。</span></p>
<p><span style="font-family: 宋体; font-size: 14px">MongoDB对于关系型数据库里的表,但是集合中没有列、行和关系概念,这体现了模式自由的特点。</span></p>
<p> </p>
<p><span style="font-family: 宋体; font-size: 14px; color: rgba(255, 0, 0, 1)"><strong>(2),特点</strong></span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 它的特点是高性能、易部署、易使用,存储数据非常方便。主要功能特性有:</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 1)面向集合存储,易存储对象类型的数据。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 2)模式自由。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 3)支持动态查询。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 4)支持完全索引,包含内部对象。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 5)支持查询。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 6)支持复制和故障恢复。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 7)使用高效的二进制数据存储,包括大型对象(如视频等)。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 8)自动处理碎片,以支持云计算层次的扩展性。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 9)支持RUBY,PYTHON,JAVA,C++,PHP,C#等多种语言。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 10)文件存储格式为BSON(一种JSON的扩展)。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> 11)可通过网络访问。</span></p>
<p> </p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong><span style="font-family: 宋体; font-size: 14px">(3),安装及使用</span></strong></span></p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong><span style="font-family: 宋体; font-size: 14px"> </span></strong><span style="font-family: 宋体; font-size: 14px"><span style="color: rgba(0, 0, 0, 1)">1,下载并解压 "mongodb" 到指定目录,如下:</span></span></span></p>
<p><span style="color: rgba(255, 0, 0, 1)"><span style="font-family: 宋体; font-size: 14px"><span style="color: rgba(0, 0, 0, 1)"> <img src="//images0.cnblogs.com/blog/703832/201501/122154282612729.png"></span></span></span></p>
<p> </p>
<p><span style="color: rgba(0, 0, 0, 1)"><span style="font-family: 宋体; font-size: 14px"> </span>2,</span>其中有两个最重要的文件:”Mongod.exe“和”Mongo.exe“ 。</p>
<p> </p>
<p> Mongod.exe ------ 用来连接到mongo数据库服务器的,即服务器端。</p>
<p> </p>
<p> Mongo.exe ------ 用来启动MongoDB shell的,即客户端。</p>
<p> </p>
<p>二,分步骤操作</p>
<p> (1),新建一个目录,例如:”>mongod -dbpath data/db“ 。</p>
<p> (2),打开浏览器输入:“http://127.0.0.1:27017/”,<span style="font-family: 宋体; font-size: 14px">见到如下字样:</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> "You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number",表示已成功。</span></p>
<p><span style="font-family: 宋体; font-size: 14px"> </span>到此,MongoDB数据库服务已经成功启动了。</p>
<p><span style="font-family: 宋体; font-size: 14px"> (3),</span>创建数据库------进入CMD,键入命令“mongo.exe” 出现如下界面:</p>
<p> <img src="//images0.cnblogs.com/blog/703832/201501/122204126677121.jpg"></p>
<p> (4),在shell 命令窗口键入如下命令:" use bb" (use 命令用来切换当前数据库,如果该数据库不存在,则会先新建一个)。</p>
<p> (5),在shell 命令窗口键入如下命令:“ db.users.insert({"name":"bigbear","password":"12345678"})”,</p>
<p> (这条命令是向users 集合中插入一条数据,如果集合users不存在,则会先新建一个,然后再插入数据,参数以JSON格式传入)。</p>
<p> (6),在shell 命令窗口键入如下命令:“db.users.find()” (显示users集合下的所有数据文档),如下图:</p>
<p> <img src="//images0.cnblogs.com/blog/703832/201501/122209503703485.jpg"></p>
<p> 注意图中的“<span style="color: rgba(255, 0, 0, 1)"><strong>_id</strong></span>”,系统给每条记录自动分配了一个惟一主键 _id 。</p>
<p> <span style="color: rgba(255, 0, 0, 1)"><strong>好了!基本的功能够用了,后面如果需要额外的操作逐步补充,由于篇幅有限主要说的是NodeJS操作Mongodb,所以暂时告一段落。</strong></span></p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong> </strong></span></p>
<p><span style="color: rgba(0, 0, 0, 1)"> 三,进入主题,实例分析</span></p>
<p><span style="color: rgba(0, 0, 0, 1)"> (1),</span><span style="color: rgba(255, 0, 0, 1)"><strong><span style="font-family: 宋体; font-size: 14px">npm install mongodb (下载后放入制定目录下)。</span></strong></span></p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong><span style="font-family: 宋体; font-size: 14px"> </span></strong><span style="font-family: 宋体; font-size: 14px; color: rgba(0, 0, 0, 1)">(2),之前我们已经建立好了数据库和集合("bb","users")</span></span></p>
<p><span style="color: rgba(255, 0, 0, 1)"><span style="font-family: 宋体; font-size: 14px; color: rgba(0, 0, 0, 1)"> </span></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">var</span> mongodb = require("mongodb"<span style="color: rgba(0, 0, 0, 1)">) ;
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 255, 1)">var</span> server = <span style="color: rgba(0, 0, 255, 1)">new</span> mongodb.Server("localhost",27017<span style="color: rgba(0, 0, 0, 1)">,{
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span> auto_reconnect : <span style="color: rgba(0, 0, 255, 1)">true</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 0, 0, 1)">}) ;
</span><span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">var</span> conn = <span style="color: rgba(0, 0, 255, 1)">new</span> mongodb.Db("bb"<span style="color: rgba(0, 0, 0, 1)">,server,{
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> safe : <span style="color: rgba(0, 0, 255, 1)">true</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 0, 1)">}) ;
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> conn.open(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(error,db){
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(error) <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> error ;
</span><span style="color: rgba(0, 128, 128, 1)">10</span> db.collection("users"<span style="color: rgba(0, 0, 0, 1)">,{
</span><span style="color: rgba(0, 128, 128, 1)">11</span> safe : <span style="color: rgba(0, 0, 255, 1)">true</span>
<span style="color: rgba(0, 128, 128, 1)">12</span> },<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(err,collection){
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(err) <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> err ;
</span><span style="color: rgba(0, 128, 128, 1)">14</span> collection.find().toArray(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(e,docs){
</span><span style="color: rgba(0, 128, 128, 1)">15</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(e) <span style="color: rgba(0, 0, 255, 1)">throw</span><span style="color: rgba(0, 0, 0, 1)"> e ;
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)"> console.log(docs) ;
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 0, 1)"> }) ;
</span><span style="color: rgba(0, 128, 128, 1)">18</span> <span style="color: rgba(0, 0, 0, 1)"> }) ;
</span><span style="color: rgba(0, 128, 128, 1)">19</span> }) ;</pre>
</div>
<p>运行结果如下:</p>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">[
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 0, 1)"> _id: 54b3ce920dc20a3ba9607f
</span><span style="color: rgba(0, 128, 128, 1)">4</span> name: 'bigbear'<span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 128, 128, 1)">5</span> password: '12345678'
<span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">7</span> ]</pre>
</div>
<p> </p>
<p>四,总结一下</p>
<p> </p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong>(1),熟练使用MongoDB数据库。</strong></span></p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong> </strong></span></p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong>(2),理解与关系型数据库之间的差异化。</strong></span></p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong> </strong></span></p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong>(3),通过上述的例子,掌握NodeJS如何操作MongoDB。</strong></span></p>
<p> </p>
<p><span style="color: rgba(255, 0, 0, 1)"><strong>(4),强调一句话:如何设计“<strong><span style="color: rgba(51, 102, 255, 1)">NoSQL</span>”类型的</strong>数据库,思想很重要。</strong></span></p>
<p> </p>
<p> </p>
<p> </p>
<p> <span style="color: rgba(0, 0, 255, 1); background-color: rgba(255, 153, 204, 1)"><strong> </strong><strong> 哈哈哈,本篇结束,未完待续,希望和大家多多交流够沟通,共同进步。。。。。。呼呼呼……(*^__^*) </strong><strong> </strong><strong> </strong></span></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/bigbearbb/p/4220102.html
頁:
[1]