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,非常easy。运行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管理。 首先要改动下port和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查询。
仅仅追求高效,高速訪问。