码迷,mamicode.com
首页 > 数据库 > 详细

《转》CentOS7 安装MongoDB 3.0服务器 (3.0的优势)

时间:2015-07-28 13:11:01      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:mongodb

1,下载&安装

MongoDB 3.0 正式版本发布!这标志着 MongoDB 数据库进入了一个全新的发展阶段,提供强大、灵活而且易于管理的数据库管理系统。MongoDB宣称,3.0新版本不只提升7到10倍的写入效率以及增加80%的数据压缩率,还能减少95%的运维成本。 
  MongoDB 3.0主要新特性包括: 
  ·可插入式的存储引擎 API 
  ·支持 WiredTiger 存储引擎 
  ·MMAPv1 提升 
  ·复制集全面提升 
  ·集群方面的改进 
  ·提升了安全性 
  ·工具的提升 
WiredTiger 存储引擎是一项难以置信的技术实现,提供无门闩、非堵塞算法来利用先进的硬件平台(如大容量芯片缓存和线程化架构)来提升性能。通过 WiredTiger,MongoDB 3.0 实现了文档级别的并发控制,因此大幅提升了大并发下的写负载。

MongoDB 提供了centos yum安装方式。 
参考:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/ 
pdf 手册: 
http://docs.mongodb.org/manual/MongoDB-manual.pdf

vi /etc/yum.repos.d/mongodb-org-3.0.repo

<code class="hljs ini has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-title" style="color:#06666;box-sizing: border-box;">[mongodb-org-3.0]</span>
<span class="hljs-setting" style="box-sizing: border-box;">name=<span class="hljs-value" style="box-sizing: border-box;">MongoDB Repository</span></span>
<span class="hljs-setting" style="box-sizing: border-box;">baseurl=<span class="hljs-value" style="box-sizing: border-box;">http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/<span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span>/x86_64/</span></span>
<span class="hljs-setting" style="box-sizing: border-box;">gpgcheck=<span class="hljs-value" style="box-sizing: border-box;"><span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span></span></span>
<span class="hljs-setting" style="box-sizing: border-box;">enabled=<span class="hljs-value" style="box-sizing: border-box;"><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span></span></span></code>

安装mongodb

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">yum install <span class="hljs-attribute" style="box-sizing: border-box;">-y</span> mongodb<span class="hljs-attribute" style="box-sizing: border-box;">-org</span></code>

安装了所有相关服务。

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">...</span><span class="hljs-attribute" style="box-sizing: border-box;">...</span>
Running transaction
  Installing : mongodb<span class="hljs-attribute" style="box-sizing: border-box;">-org</span><span class="hljs-attribute" style="box-sizing: border-box;">-shell</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">.2</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1.</span>el7<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>x86_64      <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span>/<span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span> 
  Installing : mongodb<span class="hljs-attribute" style="box-sizing: border-box;">-org</span><span class="hljs-attribute" style="box-sizing: border-box;">-tools</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">.2</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1.</span>el7<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>x86_64      <span class="hljs-number" style="color:#06666;box-sizing: border-box;">2</span>/<span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span> 
  Installing : mongodb<span class="hljs-attribute" style="box-sizing: border-box;">-org</span><span class="hljs-attribute" style="box-sizing: border-box;">-mongos</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">.2</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1.</span>el7<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>x86_64     <span class="hljs-number" style="color:#06666;box-sizing: border-box;">3</span>/<span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span> 
  Installing : mongodb<span class="hljs-attribute" style="box-sizing: border-box;">-org</span><span class="hljs-attribute" style="box-sizing: border-box;">-server</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">.2</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1.</span>el7<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>x86_64     <span class="hljs-number" style="color:#06666;box-sizing: border-box;">4</span>/<span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span> 
  Installing : mongodb<span class="hljs-attribute" style="box-sizing: border-box;">-org</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">.2</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1.</span>el7<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>x86_64            <span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span>/<span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span> 
  Verifying  : mongodb<span class="hljs-attribute" style="box-sizing: border-box;">-org</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">.2</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1.</span>el7<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>x86_64           <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span>/<span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span> 
  Verifying  : mongodb<span class="hljs-attribute" style="box-sizing: border-box;">-org</span><span class="hljs-attribute" style="box-sizing: border-box;">-server</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">.2</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1.</span>el7<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>x86_64    <span class="hljs-number" style="color:#06666;box-sizing: border-box;">2</span>/<span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span> 
  Verifying  : mongodb<span class="hljs-attribute" style="box-sizing: border-box;">-org</span><span class="hljs-attribute" style="box-sizing: border-box;">-mongos</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">.2</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1.</span>el7<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>x86_64    <span class="hljs-number" style="color:#06666;box-sizing: border-box;">3</span>/<span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span> 
  Verifying  : mongodb<span class="hljs-attribute" style="box-sizing: border-box;">-org</span><span class="hljs-attribute" style="box-sizing: border-box;">-tools</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">.2</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1.</span>el7<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>x86_64     <span class="hljs-number" style="color:#06666;box-sizing: border-box;">4</span>/<span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span> 
  Verifying  : mongodb<span class="hljs-attribute" style="box-sizing: border-box;">-org</span><span class="hljs-attribute" style="box-sizing: border-box;">-shell</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">.2</span><span class="hljs-subst" style="color:#000000;box-sizing: border-box;">-</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">1.</span>el7<span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">.</span>x86_64     <span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span>/<span class="hljs-number" style="color:#06666;box-sizing: border-box;">5</span></code>

配置文件在:/etc/mongod.conf  数据文件在:/var/lib/mongo  日志文件在:/var/log/mongodb  mongodb服务使用

<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">#启动</span>
service mongod start
<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">#停止</span>
service mongod stop
<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">#重启</span>
service mongod restart
<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">#增加开机启动</span>
chkconfig mongod on</code>

2,MongoDB CRUD

参考:  http://docs.mongodb.org/manual/core/crud-introduction/

连接到MongoDB,很简单,执行mongo就可以了。

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color:#8800;box-sizing: border-box;"># mongo</span>
MongoDB <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">shell</span> <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">version</span>: <span class="hljs-number" style="color:#06666;box-sizing: border-box;">3.0</span><span class="hljs-number" style="color:#06666;box-sizing: border-box;">.2</span>
connecting <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">to</span>: test
Welcome <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">to</span> <span class="hljs-operator" style="box-sizing: border-box;">the</span> MongoDB <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">shell</span>.
For interactive help, type <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"help"</span>.
For more comprehensive documentation, see
        <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">http</span>://docs.mongodb.org/
Questions? Try <span class="hljs-operator" style="box-sizing: border-box;">the</span> support group
        <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">http</span>://groups.google.com/group/mongodb-user
Server has startup warnings: 
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.544</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I STORAGE  [initandlisten] 
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.544</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I STORAGE  [initandlisten] ** WARNING: Readahead <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">for</span> /var/lib/mongo is <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">set</span> <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">to</span> <span class="hljs-number" style="color:#06666;box-sizing: border-box;">4096</span>KB
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.544</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I STORAGE  [initandlisten] **          We suggest setting <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">it</span> <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">to</span> <span class="hljs-number" style="color:#06666;box-sizing: border-box;">256</span>KB (<span class="hljs-number" style="color:#06666;box-sizing: border-box;">512</span> sectors) <span class="hljs-operator" style="box-sizing: border-box;">or</span> less
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.544</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I STORAGE  [initandlisten] **          <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">http</span>://dochub.mongodb.org/core/readahead
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.679</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I CONTROL  [initandlisten] 
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.679</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is <span class="hljs-string" style="color:#0880;box-sizing: border-box;">'always'</span>.
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.679</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I CONTROL  [initandlisten] **        We suggest setting <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">it</span> <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">to</span> <span class="hljs-string" style="color:#0880;box-sizing: border-box;">'never'</span>
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.679</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I CONTROL  [initandlisten] 
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.679</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is <span class="hljs-string" style="color:#0880;box-sizing: border-box;">'always'</span>.
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.679</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I CONTROL  [initandlisten] **        We suggest setting <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">it</span> <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">to</span> <span class="hljs-string" style="color:#0880;box-sizing: border-box;">'never'</span>
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.679</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I CONTROL  [initandlisten] 
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.679</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">set</span> <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">to</span> <span class="hljs-number" style="color:#06666;box-sizing: border-box;">4096</span> processes, <span class="hljs-number" style="color:#06666;box-sizing: border-box;">64000</span> <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">files</span>. Number <span class="hljs-operator" style="box-sizing: border-box;">of</span> processes should be <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">at</span> least <span class="hljs-number" style="color:#06666;box-sizing: border-box;">32000</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0.5</span> times <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">number</span> <span class="hljs-operator" style="box-sizing: border-box;">of</span> <span class="hljs-built_in" style="color:#66066;box-sizing: border-box;">files</span>.
<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">04</span>-<span class="hljs-number" style="color:#06666;box-sizing: border-box;">29</span>T18:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">03</span>:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">17.679</span>+<span class="hljs-number" style="color:#06666;box-sizing: border-box;">0800</span> I CONTROL  [initandlisten] 
> </code>

2.1,创建数据:

http://docs.mongodb.org/manual/tutorial/insert-documents/  http://docs.mongodb.org/manual/reference/method/db.collection.insert/

<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">> db.users.insert(
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> {
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> name:<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"zhang san"</span>,
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> age:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">26</span>,
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> city:<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"bei jing"</span>
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> }
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> )
WriteResult({ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nInserted"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span> })
> db.users.insert(
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> {
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> _id:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span>,
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> name:<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"zhang san"</span>,
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> age:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">26</span>,
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> city:<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"bei jing"</span>
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> }
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> )
WriteResult({ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nInserted"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span> })
> db.users.insert(
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> {
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> _id:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span>,
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> name:<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"zhang san"</span>,
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> age:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">26</span>,
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> city:<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"bei jing"</span>
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> }
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> )
WriteResult({
        <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nInserted"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>,
        <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"writeError"</span> : {
                <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"code"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">11000</span>,
                <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"errmsg"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"E11000 duplicate key error index: test.users.$_id_ dup key: { : 1.0 }"</span>
        }
})
> db.users.insert(
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> {
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> _id:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2</span>,
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> name:<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"li si"</span>,
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> age:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">28</span>,
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> city:<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"shang hai"</span>
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> }
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> )
WriteResult({ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nInserted"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span> })</code>

数据可以没有主键_id,如果没有,会自动生成一个。如果设置了_id主键,就必须不重复。  否则报主键冲突:“E11000 duplicate key error index: test.users.$_id_ dup key: { : 1.0 }”

2.2,更新数据:

http://docs.mongodb.org/manual/tutorial/modify-documents/

<code class="hljs r has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">> db.users.update(
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> {_id:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">2</span>},
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> {
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> $set: {
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> city:<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"guang zhou"</span>
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> }
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> }
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> )
WriteResult({ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nMatched"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nUpserted"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nModified"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span> })
> db.users.update(
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> {_id:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">3</span>},
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> {
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> $set: {
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> city:<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"si chuan"</span>
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> }
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> },
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> { upsert: true }
<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">...</span> )
WriteResult({ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nMatched"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nUpserted"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nModified"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"_id"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">3</span> })</code>

更新使用update,如果增加{ upsert: true },则表示没有查询到数据直接插入。

2.3,删除:

http://docs.mongodb.org/manual/tutorial/remove-documents/

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">> db<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.users</span><span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.remove</span>({_id:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">3</span>})
WriteResult({ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nRemoved"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span> })
> db<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.users</span><span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.remove</span>({_id:<span class="hljs-number" style="color:#06666;box-sizing: border-box;">4</span>})
WriteResult({ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"nRemoved"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">0</span> })</code>

查询到数据才进行删除,并且返回删除数量。

2.4,查询:

http://docs.mongodb.org/manual/tutorial/query-documents/

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">> db<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.users</span><span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.find</span>({age:{ $gt: <span class="hljs-number" style="color:#06666;box-sizing: border-box;">26</span>}})
{ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"_id"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">2</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"name"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"li si"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"age"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">28</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"city"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"guang zhou"</span> }
> db<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.users</span><span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.find</span>({age:{ $gt: <span class="hljs-number" style="color:#06666;box-sizing: border-box;">25</span>}})
{ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"_id"</span> : ObjectId(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"5540adf29b0f52a6786de216"</span>), <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"name"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"zhang san"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"age"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">26</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"city"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"bei jing"</span> }
{ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"_id"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"name"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"zhang san"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"age"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">26</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"city"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"bei jing"</span> }
{ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"_id"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">2</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"name"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"li si"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"age"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">28</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"city"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"guang zhou"</span> }
<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">#查询全部数据</span>
> db<span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.users</span><span class="hljs-preprocessor" style="color:#444444;box-sizing: border-box;">.find</span>()
{ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"_id"</span> : ObjectId(<span class="hljs-string" style="color:#0880;box-sizing: border-box;">"5540adf29b0f52a6786de216"</span>), <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"name"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"zhang san"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"age"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">26</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"city"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"bei jing"</span> }
{ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"_id"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">1</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"name"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"zhang san"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"age"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">26</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"city"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"bei jing"</span> }
{ <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"_id"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">2</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"name"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"li si"</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"age"</span> : <span class="hljs-number" style="color:#06666;box-sizing: border-box;">28</span>, <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"city"</span> : <span class="hljs-string" style="color:#0880;box-sizing: border-box;">"guang zhou"</span> }</code>

2.5,更多方法

db.collection.aggregate()  db.collection.count()  db.collection.copyTo()  db.collection.createIndex()  db.collection.getIndexStats()  db.collection.indexStats()  db.collection.dataSize()  db.collection.distinct()  db.collection.drop()  db.collection.dropIndex()  db.collection.dropIndexes()  db.collection.ensureIndex()  db.collection.explain()  db.collection.find()  db.collection.findAndModify()  db.collection.findOne()  db.collection.getIndexes()  db.collection.getShardDistribution()  db.collection.getShardVersion()  db.collection.group()  db.collection.insert()  db.collection.isCapped()  db.collection.mapReduce()  db.collection.reIndex()  db.collection.remove()  db.collection.renameCollection()  db.collection.save()  db.collection.stats()  db.collection.storageSize()  db.collection.totalSize()  db.collection.totalIndexSize()  db.collection.update()  db.collection.validate()

3,MongoDB可视化工具

http://www.robomongo.org/  技术分享

使用可视化工具,方便使用MongoDB管理。  首先要修改下端口和ip  vi /etc/mongod.conf

<code class="hljs delphi has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">port=<span class="hljs-number" style="color:#06666;box-sizing: border-box;">27017</span>

dbpath=/<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">var</span>/lib/mongo

# location <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">of</span> pidfile
pidfilepath=/<span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">var</span>/run/mongodb/mongod.pid

# Listen <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">to</span> local <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">interface</span> only. Comment <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">out</span> <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">to</span> listen <span class="hljs-keyword" style="color:#0088;box-sizing: border-box;">on</span> all interfaces.
bind_ip=<span class="hljs-number" style="color:#06666;box-sizing: border-box;">192.168</span>.<span class="hljs-number" style="color:#06666;box-sizing: border-box;">1.36</span></code>

然后重启MongoDB

<code class="hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">service mongod restart</code>

接下来就可以创建一个mongodb连接:  技术分享  连接成功之后效果:  技术分享

4,总结

本文原文连接: http://blog.csdn.net/freewebsys/article/details/45368809 转载请注明出处!

MongoDB 3.0操作起来还是很方便的。能高效的使用。  同时MongoDB扩展也很方便。接下来研究。  对应互联网业务来说没有复杂的join查询。只追求高效,快速访问。

版权声明:本文为博主原创文章,未经博主允许不得转载。

《转》CentOS7 安装MongoDB 3.0服务器 (3.0的优势)

标签:mongodb

原文地址:http://blog.csdn.net/qq_21398167/article/details/47102253

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!