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

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查询。只追求高效,快速访问。

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

时间: 2024-10-15 20:40:53

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

CentOS7 安装MongoDB 3.0服务器

CentOS7 安装MongoDB 3.0服务器 时间:2015-05-03 16:33来源:blog.csdn.net 作者:freewebsys 举报 点击:1449次 1,下载&安装 MongoDB 3.0 正式版本发布!这标志着 MongoDB 数据库进入了一个全新的发展阶段,提供强大.灵活而且易于管理的数据库管理系统.MongoDB宣称,3.0新版本不只提升7到10倍的写入效率以及增加80%的数据压缩率,还能减少95%的运维成本.  MongoDB 3.0主要新特性包括:  ·可插入式

CentOS7 安装MongoDB 3.0服务

1,下载&安装 MongoDB 3.0 正式版本发布!这标志着 MongoDB 数据库进入了一个全新的发展阶段,提供强大.灵活而且易于管理的数据库管理系统.MongoDB宣称,3.0新版本不只提升7到10倍的写入效率以及增加80%的数据压缩率,还能减少95%的运维成本. MongoDB 3.0主要新特性包括: ·可插入式的存储引擎 API ·支持 WiredTiger 存储引擎 ·MMAPv1 提升 ·复制集全面提升 ·集群方面的改进 ·提升了安全性 ·工具的提升 WiredTiger 存储引擎

《转》CentOS7 安装MongoDB 3.0server (3.0的优势)

1.下载&安装 MongoDB 3.0 正式版本号公布!这标志着 MongoDB 数据库进入了一个全新的发展阶段,提供强大.灵活并且易于管理的数据库管理系统.MongoDB宣称.3.0新版本号不仅仅提升7到10倍的写入效率以及添加80%的数据压缩率.还能降低95%的运维成本. MongoDB 3.0主要新特性包含: ·可插入式的存储引擎 API ·支持 WiredTiger 存储引擎 ·MMAPv1 提升 ·复制集全面提升 ·集群方面的改进 ·提升了安全性 ·工具的提升 WiredTiger 存

MongoDB 3.0(1):CentOS7 安装MongoDB 3.0服务

目录(?)[-] 1下载安装 2MongoDB CRUD 1创建数据 2更新数据 3删除 4查询 5更多方法 3MongoDB可视化工具 4总结 本文原文连接: http://blog.csdn.net/freewebsys/article/details/45368809 转载请注明出处! 1,下载&安装 MongoDB 3.0 正式版本发布!这标志着 MongoDB 数据库进入了一个全新的发展阶段,提供强大.灵活而且易于管理的数据库管理系统.MongoDB宣称,3.0新版本不只提升7到10倍

centos7安装mongodb

MongoDB官网:http://www.mongodb.org/ 1:安装MongoDB centos7版本的MongoDB无需安装,只要解压就能使用了.MongoDB 提供了可用于 32 位和 64 位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB 预编译二进制包下载地址:https://www.mongodb.com/download-center#community tar -zxvf mongodb-linux-x86_64-rhel70-3.6.4.tgz

Centos7 安装Mongodb的C++ driver

按照官网(https://github.com/mongodb/mongo-cxx-driver/wiki/Quickstart-Guide-(New-Driver))上说的,C++ driver内部使用了libbson和 MongoDB C driver.所以要安装C++ driver,需要先安装C driver.我的系统gcc版本4.8.5,cmake版本3.5.2,达到官网要求的安装版本. 1.安装automake, autoconf and libtool,这里全部yum安装即可 yum

CentOS7 安装 Mongodb 与 NodeJs 主要心得

一.mongodb 1.安装 由于使用yum源下载安装总是超时,所以选择了tarball方式安装. 官方安装方法链接,https://docs.mongodb.com/manual/tutorial/install-mongodb-on-linux/ 2.使用 书籍方面,可以参考<MongoDB_The_Definitive_Guide>,主要讲解使用Mongo Shell工具的增删改查等,有许多用例. 更全面的使用方法,可以参考官方文档:https://docs.mongodb.com/ma

centos7 安装 gitolite (git服务器)

gitolite简介 轻量级git服务器程序,解决了git权限管理的问题.(git是一个分布式版本控制系统,就是说每个人作为客户端的同时又是服务器)项目GitHub地址:https://github.com/sitaramc/gitolite项目官方文档:http://gitolite.com/gitolite/当前环境:centos 7 安装前准备 在客户端机器安装git,并生成秘钥各操作系统安装方法均很简单,请自行安装.使用git安装目录下的 usr/bin/ssh-keygen生成rsa秘

centos7 安装mongodb

1.下载安装包 curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.12.tgz 2.解压 tar -zxvf mongodb-linux-x86_64-3.2.12.tgz 3.移动到指定位置 mv mongodb-linux-x86_64-3.2.12/ /usr/local/mongodb 4.在/usr/local/mongodb下创建文件夹 mkdir -p /data/db mkdir /logs 5.在