欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

Linux下的Mongodb部署應用梳理

一、MongoDB簡介 官網(wǎng)地址:http://www.mongodb.org/

呼中網(wǎng)站建設公司創(chuàng)新互聯(lián),呼中網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為呼中上千余家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的呼中做網(wǎng)站的公司定做!

MongoDB是一個高性能,開源,無模式的文檔型數(shù)據(jù)庫,是當前NoSql數(shù)據(jù)庫中比較熱門的一種。MongoDB 是一個介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當中功能
最豐富,最像關(guān)系數(shù)據(jù)庫的。它在許多場景下可用于替代傳統(tǒng)的關(guān)系型數(shù)據(jù)庫或鍵/值存儲方式。它是由C++語言編寫的一個基于分布式文件存儲的開源數(shù)據(jù)庫系統(tǒng),它的目的在于為WEB應
用提供可擴展的高性能數(shù)據(jù)存儲解決方案。MongoDB是一個介于關(guān)系型數(shù)據(jù)庫和非關(guān)系型數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系型數(shù)據(jù)庫當中功能最豐富,最像關(guān)系型數(shù)據(jù)庫的。它支持的數(shù)據(jù)結(jié)構(gòu)
非常松散,會將數(shù)據(jù)存儲為一個文檔,數(shù)據(jù)結(jié)構(gòu)由鍵值對(key=>value)組成,是類似于json的bson格式,字段值可以包含其它文檔、數(shù)組和文檔數(shù)組,因此可以存儲比較復雜的數(shù)據(jù)類型。
二、Mongodb特點

MongoDB特點是高性能、易部署、易使用,存儲數(shù)據(jù)非常方便,最大的特點在于它支持的查詢語言非常強大,其語法有點類似于面向?qū)ο蟮牟樵冋Z言,幾乎可以實現(xiàn)類似關(guān)系型數(shù)據(jù)庫單表
查詢的絕大部分功能,而且還支持對數(shù)據(jù)建立索引。MongoDB的主要特點總結(jié)如下:
1)提供了一個面向集合的文檔存儲,易存儲對象類型的數(shù)據(jù),操作起來比較簡單和容易的非關(guān)系型數(shù)據(jù)庫
2)使用update()命令可以實現(xiàn)替換完成的文檔(數(shù)據(jù))或者一些指定的數(shù)據(jù)字段。
3)支持動態(tài)查詢。
4)支持完全索引,包含內(nèi)部對象,可以在MongoDB記錄中設置任何屬性的索引來實現(xiàn)更快的排序。
5)支持復制和故障恢復。
6)使用高效的二進制數(shù)據(jù)存儲,包括大型對象(如視頻等)。
7)GridFS是MongoDB中的一個內(nèi)置功能,可以用于存放大量小文件。
8)自動處理碎片,以支持云計算層次的擴展性;如果負載的增加(需要更多的存儲空間和更強的處理能力),它可以分布在計算機網(wǎng)絡中的其它節(jié)點上,這就是所謂的分片。
9)支持RUBY,PYTHON,JAVA,C++,PHP,C#等多種語言。
10)文件存儲格式為BSON(一種JSON的擴展),MongoDB支持豐富的查詢表達式,查詢指令使用JSON形式的標記,可輕易查詢文檔中內(nèi)嵌的對象和數(shù)組。
11)MongoDB允許在服務端執(zhí)行腳本,可以用JavaScript編寫某個函數(shù),直接在服務端執(zhí)行,也可以吧函數(shù)的定義存儲在服務端,下次直接調(diào)用即可。
12)可通過網(wǎng)絡訪問,可以通過本地u或者網(wǎng)絡創(chuàng)建數(shù)據(jù)鏡像,這使得MongoDB含有更強的擴展性。
Mongodb的不足之處

1)在集群分片中的數(shù)據(jù)分布不均勻
2)單機可靠性比較差
3)大數(shù)據(jù)量持續(xù)插入,寫入性能有較大波動
4)不支持事務操作。所以事務要求嚴格的系統(tǒng)(如果銀行系統(tǒng))肯定不能用它。
5)磁盤空間占用比較大??臻g占用大的原因如下:
1-> 空間的預分配:為避免形成過多的硬盤碎片,mongodb 每次空間不足時都會申請生成一大塊的硬盤空 間,而且申請的量從 64M、128M、256M 那樣的指數(shù)遞增,直到2G為單個文件
的最大體積。隨著數(shù)據(jù)量 的增加,你可以在其數(shù)據(jù)目錄里看到這些整塊生成容量不斷遞增的文件。
2-> 字段名所占用的空間:為了保持每個記錄內(nèi)的結(jié)構(gòu)信息用于查詢,mongodb 需要把每個字段的 key-value 都以 BSON 的形式存儲,如果 value 域相對于 key 域并不大,比如
存放數(shù)值型的數(shù)據(jù),則數(shù)據(jù)的 overhead 是最大的。一種減少空間占用的方法是把字段名盡量取短一些,這樣占用 空間就小了,但這就要求在易讀 性與空間占用上作為權(quán)衡了。
3-> 刪除記錄不釋放空間:這很容易理解,為避免記錄刪除后的數(shù)據(jù)的大規(guī)模挪動,原記錄空間不刪除,只 標記“已刪除”即可,以后還可以重復利用。
4-> 可以定期運行 db.repairDatabase()來整理記錄,但這個過程會比較緩慢
三、Mongodb功能

1)面向集合的存儲:適合存儲對象及JSON形式的數(shù)據(jù)。
2)動態(tài)查詢:Mongo支持豐富的查詢表達式。查詢指令使用JSON形式的標記,可輕易查詢文檔中內(nèi)嵌的對象及數(shù)組。
3)完整的索引支持:包括文檔內(nèi)嵌對象及數(shù)組。Mongo的查詢優(yōu)化器會分析查詢表達式,并生成一個高效的查詢計劃。
4)查詢監(jiān)視:Mongo包含一個監(jiān)視工具用于分析數(shù)據(jù)庫操作的性能。
5)復制及自動故障轉(zhuǎn)移:Mongo數(shù)據(jù)庫支持服務器之間的數(shù)據(jù)復制,支持主-從模式及服務器之間的相互復制。復制的主要目標是提供冗余及自動故障轉(zhuǎn)移。
6)高效的傳統(tǒng)存儲方式:支持二進制數(shù)據(jù)及大型對象(如照片或圖片)
7)自動分片以支持云級別的伸縮性:自動分片功能支持水平的數(shù)據(jù)庫集群,可動態(tài)添加額外的機器。
四、Mongodb使用場景

適用場景:
網(wǎng)站實時數(shù)據(jù)處理。它非常適合實時的插入、更新與查詢,并具備網(wǎng)站實時數(shù)據(jù)存儲所需的復制及高度伸縮性;緩存,由于性能很高,它適合作為信息基礎設施的緩存層。在系統(tǒng)重啟之后,
由它搭建的持久化緩存層可以避免下層的數(shù)據(jù)源過載。高伸縮性的場景。非常適合由數(shù)十或數(shù)百臺服務器組成的數(shù)據(jù)庫,它的路線圖中已經(jīng)包含對MapReduce引擎的內(nèi)置支持。

不適用場景:
要求高度事務性的系統(tǒng)。傳統(tǒng)的商業(yè)智能應用。復雜的跨文檔(表)級聯(lián)查詢。
五、Mongodb安裝使用 官網(wǎng)下載地址:http://www.mongodb.org/downloads

1)安裝mongodb
[root@data-server src]# cd /usr/local/src/
[root@data-server src]# tar -zvxf mongodb-linux-x86_64-rhel62-3.4.4
[root@data-server src]# mv mongodb-linux-x86_64-rhel62-3.4.4 /usr/local/mongodb
[root@data-server src]# cd /usr/local/mongodb //Mongodb主目錄
[root@data-server mongodb]# ll
總用量 120
drwxr-xr-x. 2 root root 4096 6月 3 14:51 bin
-rw-r--r--. 1 root root 34520 4月 21 06:19 GNU-AGPL-3.0
-rw-r--r--. 1 root root 16726 4月 21 06:19 MPL-2
-rw-r--r--. 1 root root 1359 4月 21 06:19 README
-rw-r--r--. 1 root root 55625 4月 21 06:19 THIRD-PARTY-NOTICES
[root@data-server mongodb]# mkdir /usr/local/mongodb/data //Mongodb數(shù)據(jù)目錄,可以存放在一個獨立的大分區(qū)上
[root@data-server mongodb]# mkdir /usr/local/mongodb/log //Mongodb日志目錄

2)啟動Mongodb
使用mongod命令建立一個mongodb數(shù)據(jù)庫鏈接,端口號設置為10001,數(shù)據(jù)庫的路徑為/usr/local/mongodb/data,日志路徑為/usr/local/mongodb/log/mogodb.log
mongodb的啟動程序放在后臺執(zhí)行,下面命令執(zhí)行后,按ctrl+c
[root@data-server mongodb]# nohup /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/mongo.log &

mongodb默認端口是27017
[root@data-server mongodb]# ps -ef|grep mongodb
root 14858 14518 1 15:01 pts/1 00:00:01 /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/mongo.log
root 14887 14518 0 15:02 pts/1 00:00:00 grep mongodb
[root@data-server bin]# netstat -tunlp|grep 14858
tcp 0 0 0.0.0.0:27017 0.0.0.0: LISTEN 14858/mongod
[root@data-server mongodb]# lsof -i:27017
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 14858 root 7u IPv4 145311 0t0 TCP
:27017 (LISTEN)

3)設置mongodb的環(huán)境變量
[root@data-server ~]# vim /etc/profile
.......
export PATH=$PATH:/usr/local/mongodb/bin/
[root@data-server ~]#source /etc/profile

4)為了更方便的啟動和關(guān)閉MongoDB,我們可以使用Shell寫腳本,當然也可以加入到service中
更好的方式是采用配置文件,把MongoDB需要的參數(shù)寫入配置文件,然后在腳本中引用;
[root@data-server ~]# cat /usr/local/mongodb/mongodb.conf
#代表端口號,如果不指定則默認為27017
port=27017
#MongoDB數(shù)據(jù)文件目錄
dbpath=/usr/local/mongodb/data
#MongoDB日志文件目錄
logpath=/usr/local/mongodb/log/mongo.log
#日志文件自動累加
logappend=true

編寫啟動腳本
[root@data-server ~]# vim /etc/init.d/mongodb
#!/bin/bash
#mongod Start up the MongoDB server daemon
#source function library
. /etc/rc.d/init.d/functions
#定義命令
CMD=/usr/local/mongodb/bin/mongod
#定義配置文件路徑
INITFILE=/usr/local/mongodb/mongodb.conf
start()
{
#&表示后臺啟動,也可以使用fork參數(shù)
$CMD -f $INITFILE &
echo "MongoDB is running background..."
}

stop()
{
pkill mongod
echo "MongoDB is stopped."
}

case "$1" in
start)
start
;;
stop)
stop
;;
)
echo $"Usage: $0 {start|stop}"
esac
[root@data-server ~]# chmod 755 /etc/init.d/mongodb
[root@data-server ~]# /etc/init.d/mongodb status
用法:/etc/init.d/mongodb {start|stop}
[root@data-server ~]# /etc/init.d/mongodb stop
已終止
[root@data-server ~]# lsof -i:27017
[root@data-server ~]# /etc/init.d/mongodb start
MongoDB is running background...
[root@data-server ~]# lsof -i:27017
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 15138 root 7u IPv4 147713 0t0 TCP
:27017 (LISTEN)

通過 shell 連接 MongoDB 服務:(在客戶機上連接本機mongodb:mongo 182.48.115.238:27017)
[root@data-server ~]# mongo
MongoDB shell version v3.4.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2017-06-03T15:17:54.695+0800 I STORAGE [initandlisten]
2017-06-03T15:17:54.695+0800 I STORAGE [initandlisten] WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2017-06-03T15:17:54.695+0800 I STORAGE [initandlisten]
See http://dochub.mongodb.org/core/prodnotes-filesystem
2017-06-03T15:17:55.699+0800 I CONTROL [initandlisten]
2017-06-03T15:17:55.700+0800 I CONTROL [initandlisten] WARNING: Access control is not enabled for the database.
2017-06-03T15:17:55.700+0800 I CONTROL [initandlisten]
Read and write access to data and configuration is unrestricted.
2017-06-03T15:17:55.700+0800 I CONTROL [initandlisten] WARNING: You are running this process as the root user, which is not recommended.
2017-06-03T15:17:55.700+0800 I CONTROL [initandlisten]
2017-06-03T15:17:55.700+0800 I CONTROL [initandlisten]
2017-06-03T15:17:55.700+0800 I CONTROL [initandlisten]
WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-06-03T15:17:55.700+0800 I CONTROL [initandlisten] We suggest setting it to 'never'
2017-06-03T15:17:55.700+0800 I CONTROL [initandlisten]
2017-06-03T15:17:55.700+0800 I CONTROL [initandlisten]
WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-06-03T15:17:55.700+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-06-03T15:17:55.700+0800 I CONTROL [initandlisten]

help
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce

show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell

-------------------------------------------------mongodb非正常關(guān)閉導致啟動失敗問題-------------------------------------------------

之前強制關(guān)閉了mongodb,后續(xù)再次啟動的時候,發(fā)現(xiàn)報錯:
[root@i-omxpbsuo ~]# /usr/local/mongodb/bin/mongod --logpath /usr/local/mongodb/log/system.log --logappend --dbpath /data/mongodb --directoryperdb --auth --journal --profile=1 --slowms=5 --fork
forked process: 4853
all output going to: /usr/local/mongodb/log/system.log

查看日志

這是由于上次mongodb非正常關(guān)閉導致的。
解決辦法:
1)刪除數(shù)據(jù)目錄/data/mongodb 下的 mongod.lock
[root@i-omxpbsuo ~]# rm -rf /data/mongodb/mongod.lock

2)修復mongodb
[root@i-omxpbsuo ~]# /usr/local/mongodb/bin/mongod --logpath --dbpath /data/mongodb --repair

3)刪除/data/mongodb/journal 下的 j._4 文件(或者將journal下的文件清空)
[root@i-omxpbsuo ~]# rm -rf /data/mongodb/journal/*j._4

4)然后再次啟動mongodb就ok了
[root@i-omxpbsuo ~]# /usr/local/mongodb/bin/mongod --logpath --dbpath /data/mongodb --directoryperdb --auth --journal --profile=1 --slowms=5 --fork &


正確關(guān)閉mongod 的方法:進入mongo shell

use admin
db.shutdownServer()

也可以按照文檔粗暴的殺掉它,它內(nèi)部應該有KILL信號處理程序。

killall mongod

請不要 kill -9 ,會造成文件數(shù)據(jù)混亂丟失repair也無力回天。


也可以采用yum的方式安裝mongodb,操作記錄如下:

1)創(chuàng)建repo
[root@data-server ~]# vim /etc/yum.repos.d/mongodb-org-3.2.repo
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=0
enabled=1

2)安裝MongoDB和相關(guān)工具
[root@data-server ~]# yum install -y mongodb-org

3)啟動Mongodb
[root@data-server ~]# service mongod start
Starting mongod: [ OK ]
[root@data-server ~]# chkconfig mongod on
[root@data-server ~]# lsof -i:27017
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 26130 mongod 6u IPv4 2773161 0t0 TCP localhost:27017 (LISTEN)

日志文件:/var/log/mongodb/mongod.log
配置文件:/etc/mongod.conf
數(shù)據(jù)目錄:/var/lib/mongo/

配置文件解釋:
[root@data-server ~]# cat /etc/mongod.conf

mongo.conf

#where to log
logpath=/var/log/mongo/mongod.log

logappend=true #以追加方式寫入日志

fork and run in background

fork = true

#port = 27017 #端口

dbpath=/var/lib/mongo #數(shù)據(jù)庫文件保存位置
directoryperdb=true

Enables periodic logging of CPU utilization and I/O wait

#啟用定期記錄CPU利用率和 I/O 等待
#cpu = true

Turn on/off security. Off is currently the default

是否以安全認證方式運行,默認是不認證的非安全方式

#noauth = true
#auth = true

Verbose logging output.

詳細記錄輸出

#verbose = true

Inspect all client data for validity on receipt (useful for

developing drivers)用于開發(fā)驅(qū)動程序時的檢查客戶端接收數(shù)據(jù)的有效性

#objcheck = true

Enable db quota management 啟用數(shù)據(jù)庫配額管理,默認每個db可以有8個文件,可以用quotaFiles參數(shù)設置

#quota = true

設置oplog記錄等級

Set oplogging level where n is

0=off (default)

1=W

2=R

3=both

7=W+some reads

#oplog = 0

Diagnostic/debugging option 動態(tài)調(diào)試項

#nocursors = true

Ignore query hints 忽略查詢提示

#nohints = true

禁用http界面,默認為localhost:28017

Disable the HTTP interface (Defaults to localhost:27018).這個端口號寫的是錯的

#nohttpinterface = true

關(guān)閉服務器端腳本,這將極大的限制功能

Turns off server-side scripting. This will result in greatly limited

functionality

#noscripting = true

關(guān)閉掃描表,任何查詢將會是掃描失敗

Turns off table scans. Any query that would do a table scan fails.

#notablescan = true

關(guān)閉數(shù)據(jù)文件預分配

Disable data file preallocation.

#noprealloc = true

為新數(shù)據(jù)庫指定.ns文件的大小,單位:MB

Specify .ns file size for new databases.

nssize = <size>

Accout token for Mongo monitoring server.

#mms-token = <token>

mongo監(jiān)控服務器的名稱

Server name for Mongo monitoring server.

#mms-name = <server-name>

mongo監(jiān)控服務器的ping 間隔

Ping interval for Mongo monitoring server.

#mms-interval = <seconds>

Replication Options 復制選項

in replicated mongo databases, specify here whether this is a slave or master 在復制中,指定當前是從屬關(guān)系

#slave = true
#source = master.example.com

Slave only: specify a single database to replicate

#only = master.example.com

or

#master = true
#source = slave.example.com
六、Mongodb的日常操作命令

1)登陸和退出..........................................
mongo 命令直接加MongoDB服務器的IP地址就可以利用 Mongo 的默認端口號(27017)登陸 Mongo,然后便能夠進行簡單的命令行操作。至于退出,直接 exit,然后回車就好了。

如果默認bind綁定的ip是127.0.0.1以及端口是27017,那么登陸可以直接用下面兩種都可以:
[root@master-node ~]# mongo
[root@master-node ~]# mongo 127.0.0.1:27017

如果是綁定了固定的ip地址,如bing_ip=182.48.115.236,那么連接mongodb命令是:
[root@master-node ~]# mongo 182.48.115.238:27017

如果是連接某個庫,命令是
[root@master-node ~]# mongo ip:port/庫名 -u用戶名 -p密碼

help
登陸mongodb數(shù)據(jù)庫后,可以直接執(zhí)行help命令進行幫助查看

show collections
從以上可以看出,登錄后 mongo 會自動連上一個名為 test 的數(shù)據(jù)庫。如果這個數(shù)據(jù)庫不存 在,那么 mongo 會自動建立一個名為 test 的數(shù)據(jù)庫。上面的例子,由于 Mongo 服務器上沒 有名為 test 的 db,因此,mongo 新建了一個空的名為 test 的 db。其中,沒有任何 collection。

2)database級操作..........................................

2.1 查看服務器上的數(shù)據(jù)庫

show dbs
admin 0.000GB
local 0.000GB
master_slave 0.000GB
wangshibo 0.000GB

2.2 切換數(shù)據(jù)庫
切換到wangshibo數(shù)據(jù)庫(從默認的 test 數(shù)據(jù)庫)

use wangshibo;
switched to db wangshibo;
mongo 中,db 代表當前使用的數(shù)據(jù)庫。這樣,db 就從原來的 test,變?yōu)楝F(xiàn)在的 wangshibo 數(shù)據(jù)庫,如果沒有這個庫,就會自動創(chuàng)建

2.3 查看當前數(shù)據(jù)庫中的所有集合

show collections
persons
test

2.4 創(chuàng)建數(shù)據(jù)庫
mongo 中創(chuàng)建數(shù)據(jù)庫采用的也是 use 命令,如果 use 后面跟的數(shù)據(jù)庫名不存在,那么 mongo 將會新建該數(shù)據(jù)庫。
不過,實際上只執(zhí)行 use 命令后,mongo 是不會新建該數(shù)據(jù)庫的,直到 你像該數(shù)據(jù)庫中插入了數(shù)據(jù)。

use huanqiu
switched to db huanqiu
show dbs
admin 0.000GB
local 0.000GB
master_slave 0.000GB
wangshibo 0.000GB
到這里并沒有看到剛才新建的 huanqiu 數(shù)據(jù)庫。

db.huanqiu.insert({"name":"testdb"})
WriteResult({ "nInserted" : 1 })
該操作會在 huanqiu 數(shù)據(jù)庫中新建一個 hello 集合,并在其中插入一條記錄。

show dbs
admin 0.000GB
huanqiu 0.000GB
local 0.000GB
master_slave 0.000GB
wangshibo 0.000GB
這樣,便可以看到 mongo 的確創(chuàng)建了 huanqiu 數(shù)據(jù)庫,其中有一個 hello 集合。

2.5 刪除數(shù)據(jù)庫
刪除當前所在庫。比如這里已經(jīng)切換到huanqiu庫,那么就刪除huanku庫

db.dropDatabase()
{ "dropped" : "huanqiu", "ok" : 1 }

show dbs
admin 0.000GB
local 0.000GB
master_slave 0.000GB
wangshibo 0.000GB

2.6 查看當前數(shù)據(jù)庫

db
huanqiu

db.getName(); //這個上面的命令是一樣的
wangshibo

可以看出刪除 huanqiu 數(shù)據(jù)庫之后,當前的 db 還是指向它,只有當切換數(shù)據(jù)庫之后,huanqiu 才會徹底消失。

use wangshibo
switched to db wangshibo
db
wangshibo
show dbs
admin 0.000GB
local 0.000GB
master_slave 0.000GB
wangshibo 0.000GB

2.6.1 修復當前數(shù)據(jù)庫

db.repairDatabase();
{ "ok" : 1 }

2.6.2 查看當前數(shù)據(jù)庫狀態(tài)

db.stats();
{
"db" : "wangshibo",
"collections" : 3,
"views" : 0,
"objects" : 6,
"avgObjSize" : 75.5,
"dataSize" : 453,
"storageSize" : 73728,
"numExtents" : 0,
"indexes" : 4,
"indexSize" : 53248,
"ok" : 1
}

2.6.3 當前db版本

db.version();
3.4.4

2.6.4 查看當前db的鏈接機器地址

db.getMongo();
connection to 182.48.115.238:27017

2.6.5 從指定主機上克隆數(shù)據(jù)庫

db.cloneDatabase("182.48.115.236");
{
"clonedColls" : [ ],
"ok" : 0,
"errmsg" : "a collection 'wangshibo.wangshibo' already exists",
"code" : 48,
"codeName" : "NamespaceExists"
}

2.6.6 從指定的機器上復制指定數(shù)據(jù)庫數(shù)據(jù)到某個數(shù)據(jù)庫
將本機的master_slave的數(shù)據(jù)復制到wangshibo數(shù)據(jù)庫中

db.copyDatabase("master_slave", "wangshibo", "127.0.0.1");
{ "ok" : 1 }

3)collection 級操作..........................................

3.1 新建 collection

db.createCollection("Hello")
{ "ok" : 1 }
show collections
Hello
wangshibo

直接向一個不存在的 collection 中插入數(shù)據(jù)也能創(chuàng)建一個 collection。

db.hello2.insert({"name":"lfqy"})
WriteResult({ "nInserted" : 1 })
show collections
Hello
hello2
wangshibo

3.2 刪除 collection

db.Hello.drop()
true
返回 true 說明刪除成功,false 說明沒有刪除成功。

db.Hello.drop()
false
不存在名為 hello 的 collection,因此,刪除失敗。

3.3 重命名 collection
將 hello2 集合重命名為 HELLO

show collections
hello2
wangshibo
db.hello2.renameCollection("HELLO")
{ "ok" : 1 }
show collections
HELLO
wangshibo

3.4 查看當前數(shù)據(jù)庫中的所有 collection

show collections
HELLO
wangshibo

3.5 建立索引在 HELLO 集合上,建立對 ID 字段的索引,1 代表升序。

db.HELLO.ensureIndex({ID:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}

4)Record 級的操作................................

4.1 插入操作
4.1.1 向 user 集合中插入兩條記錄

db.user.insert({'name':'GalGadot','gender':'female','age':28,'salary':11000})
WriteResult({ "nInserted" : 1 })
db.user.insert({'name':'Mikie Hara','gender':'female','age':26,'salary':7000})
WriteResult({ "nInserted" : 1 })

4.1.2 同樣也可以用 save 完成類似的插入操作

db.user.save({'name':'Wentworth Earl Miller','gender':'male','age':41,'salary':33000})
WriteResult({ "nInserted" : 1 })

4.2 查找操作
4.2.1 查找集合中的所有記錄

db.user.find()
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }

4.2.2 查找集合中的符合條件的記錄
(1)單一條件
查詢 age 為26 的數(shù)據(jù)

db.user.find({"age":26})
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }

查詢 salary 大于 7000 的數(shù)據(jù)

db.user.find({salary:{$gt:7000}})
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }

查詢 name 中包含'ent'的數(shù)據(jù)

db.user.find({name:/ent/})
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }

查詢 name 以 G 打頭的數(shù)據(jù)

db.user.find({name:/^G/})
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }

查詢 name 以 t 結(jié)尾的數(shù)據(jù)

db.user.find({name:/t$/})
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }

(2)多條件"與"
查詢 age 小于 30,salary 大于 7000 的數(shù)據(jù)

db.user.find({age:{$lt:30},salary:{$gt:7000}})
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }

(3)多條件"或"
查詢 age 小于 30,或者 salary 大于 10000 的記錄

db.user.find({$or:[{salary:{$gt:10000}},{age:{$lt:30}}]})
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }

4.2.3 查詢第一條記錄
將上面的 find 替換為 findOne()可以查找符合條件的第一條記錄。

db.user.findOne({$or:[{salary:{$gt:10000}},{age:{$lt:25}}]})
{
"_id" : ObjectId("59328c8aa7865327915046ae"),
"name" : "GalGadot",
"gender" : "female",
"age" : 28,
"salary" : 11000
}

4.2.4 查詢記錄的指定字段
查詢 user 集合中所有記錄的 name,age,salary,sex_orientation 字段

db.user.find({},{name:1,age:1,salary:1,sex_orientation:true})
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "age" : 41, "salary" : 33000 }

注意:這里的 1 表示顯示此列的意思,也可以用 true 表示。

db.user.find({},{name:1,age:1,salary:true,sex_orientation:1})
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "age" : 41, "salary" : 33000 }

4.2.5 查詢指定字段的數(shù)據(jù),并去重。
查詢 gender 字段的數(shù)據(jù),并去掉重復數(shù)據(jù)

db.user.distinct('gender')
[ "female", "male" ]

4.2.6 對查詢結(jié)果集的操作
(1)Pretty Print
為了方便,mongo 也提供了 pretty print 工具,db.collection.pretty()或者是 db.collection.forEach(printjson)

db.user.find().pretty()
{
"_id" : ObjectId("59328c8aa7865327915046ae"),
"name" : "GalGadot",
"gender" : "female",
"age" : 28,
"salary" : 11000
}
{
"_id" : ObjectId("59328c9da7865327915046af"),
"name" : "Mikie Hara",
"gender" : "female",
"age" : 26,
"salary" : 7000
}
{
"_id" : ObjectId("59328cd6a7865327915046b0"),
"name" : "Wentworth Earl Miller",
"gender" : "male",
"age" : 41,
"salary" : 33000
}

(2)指定結(jié)果集顯示的條目
a)顯示結(jié)果集中的前 3 條記錄

db.user.find().limit(3)
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }

b)查詢第 1 條以后的所有數(shù)據(jù)

db.user.find().skip(1)
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }

查詢跳過前2條以后的所有數(shù)據(jù)

db.user.find().skip(2)
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }

c)對結(jié)果集排序
升序

db.user.find().sort({salary:1})
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }

降序

db.user.find().sort({salary:-1})
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }

4.2.7 統(tǒng)計查詢結(jié)果中記錄的條數(shù)
(1)統(tǒng)計集合中的所有記錄條數(shù)

db.user.find().count()
3

(2)查詢符合條件的記錄數(shù)
查詢 salary 小于 4000 或大于 10000 的記錄數(shù)

db.user.find({$or: [{salary: {$lt:4000}}, {salary: {$gt:10000}}]}).count()
2

4.3 刪除操作
4.3.1 刪除整個集合中的所有數(shù)據(jù)

db.wangshibo.find()
{ "_id" : ObjectId("5932683b156e298477cdf3ad"), "name" : "菜鳥教程" }
db.wangshibo.remove({})
WriteResult({ "nRemoved" : 1 })
db.wangshibo.find()

可見 wangshibo中的記錄全部被刪除。

mongo在刪除數(shù)據(jù)的時候不支持 all * 全部刪除選擇{}就可以全部刪除了
刪除mongodb集合中的數(shù)據(jù)可以使用remove()函數(shù)。remove()函數(shù)可以接受一個查詢文檔作為可選參數(shù)來有選擇性的刪除符合條件的文檔。
remove()函數(shù)不會刪除集合本身,同時,原有的索引也同樣不會被刪除。
刪除文檔是永久性的,不能撤銷,也不能恢復的。因此,在執(zhí)行remove()函數(shù)前先用find()命令來查看下是否正確,是個比較好的習慣啦。

注意 db.collection.remove()和 drop()的區(qū)別,remove()只是刪除了集合中所有的記錄, 而集合中原有的索引等信息還在,而
drop()則把集合相關(guān)信息整個刪除(包括索引)。

4.3.2 刪除集合中符合條件的所有記錄

db.user.find()
{ "_id" : ObjectId("59328c8aa7865327915046ae"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
db.user.remove({name:'GalGadot'})
WriteResult({ "nRemoved" : 1 })
db.user.find()
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }

4.3.3 刪除集合中符合條件的一條記錄

db.user.find()
{ "_id" : ObjectId("59328c9da7865327915046af"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
db.user.remove({salary :{$lt:30000}},1)
WriteResult({ "nRemoved" : 1 })
db.user.find()
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }

當然,也可以是命令:db.user.remove({salary :{$lt:30000}},true),因為true和1是一個意思

4.4 更新操作
4.4.1 賦值更新
db.collection.update(criteria, objNew, upsert, multi )
criteria:update 的查詢條件,類似 sql update 查詢內(nèi) where 后面的
objNew:update 的對象和一些更新的操作符(如$,$inc...)等,也可以理解為 sql update 查詢內(nèi) set 后面的。
upsert : 如果不存在 update 的記錄,是否插入 objNew,true 為插入,默認是 false,不插 入。
multi : mongodb 默認是 false,只更新找到的第一條記錄,如果這個參數(shù)為 true,就把按條 件查出來多條記錄全部更新。

db.user.find()
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("593293e2a7865327915046b2"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("593293f0a7865327915046b3"), "name" : "Gasdfdst", "gender" : "female", "age" : 58, "salary" : 60000 }
{ "_id" : ObjectId("59329401a7865327915046b4"), "name" : "huihui", "gender" : "female", "age" : 18, "salary" : 23100 }
db.user.update({name:'huihui'},{$set:{age:23}},false,true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.user.find()
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("593293e2a7865327915046b2"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("593293f0a7865327915046b3"), "name" : "Gasdfdst", "gender" : "female", "age" : 58, "salary" : 60000 }
{ "_id" : ObjectId("59329401a7865327915046b4"), "name" : "huihui", "gender" : "female", "age" : 23, "salary" : 23100 }

db.user.update({name:'lfqy1'},{$set:{age:23}},true,true)
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("5932946c9758703fe04b0f73")
})
db.user.find()
{ "_id" : ObjectId("59328cd6a7865327915046b0"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("593293e2a7865327915046b2"), "name" : "GalGadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("593293f0a7865327915046b3"), "name" : "Gasdfdst", "gender" : "female", "age" : 58, "salary" : 60000 }
{ "_id" : ObjectId("59329401a7865327915046b4"), "name" : "huihui", "gender" : "female", "age" : 23, "salary" : 23100 }
{ "_id" : ObjectId("5932946c9758703fe04b0f73"), "name" : "lfqy1", "age" : 23 }

4.4.2 增值更新

db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11000 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7000 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 2 }

db.user.update({gender:'female'},{$inc:{salary:50}},false,true)

db.user.find()
{ "_id" : ObjectId("52453cfb25e437dfea8fd4f4"), "name" : "Gal Gadot", "gender" : "female", "age" : 28, "salary" : 11050 }
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female", "age" : 26, "salary" : 7050 }
{ "_id" : ObjectId("52453e2125e437dfea8fd4f6"), "name" : "Wentworth Earl Miller", "gender" : "male", "age" : 41, "salary" : 33000 }
{ "_id" : ObjectId("52454155d8947fb70d000000"), "name" : "not known", "sex_orientation" : "male", "age" : 13, "salary" : 30000 }
{ "_id" : ObjectId("5245610881c83a5bf26fc285"), "age" : 23, "name" : "lfqy1" }
{ "_id" : ObjectId("52455f8925e437dfea8fd4fd"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 1 }
{ "_id" : ObjectId("5245607525e437dfea8fd4fe"), "age" : 23, "gender" : "male", "interest" : "NBA", "name" : "lfqy", "salary" : 2 }
關(guān)于更新操作(db.collection.update(criteria, objNew, upsert, multi )),要說明的 是,如果 upsert 為 true,那么在沒有找到符合更新條件的情況下,mongo 會在集合中插入 一條記錄其值滿足更新條件的記錄(其中的字段只有更新條件中涉及的字段,字段的值滿足 更新條件),然后將其更新(注意,如果更新條件是$lt 這種不等式條件,那么 upsert 插入


其它命令操作:
1、創(chuàng)建一個聚集集合(table)
db.createCollection(“collName”, {size: 20, capped: 5, max: 100});//創(chuàng)建成功會顯示{“ok”:1}
//判斷集合是否為定容量db.collName.isCapped();

2、得到指定名稱的聚集集合(table)
db.getCollection("account");

3、得到當前db的所有聚集集合
db.getCollectionNames();

4、顯示當前db所有聚集索引的狀態(tài)
db.printCollectionStats();

三、用戶相關(guān)
1、添加一個用戶
db.addUser("name");
db.addUser("userName", "pwd123", true); 添加用戶、設置密碼、是否只讀

2、數(shù)據(jù)庫認證、安全模式
db.auth("userName", "123123");

3、顯示當前所有用戶
show users;

4、刪除用戶
db.removeUser("userName");

四、聚集集合查詢
1、查詢所有記錄
db.userInfo.find();
相當于:select* from userInfo;
默認每頁顯示20條記錄,當顯示不下的情況下,可以用it迭代命令查詢下一頁數(shù)據(jù)。注意:鍵入it命令不能帶“;”
但是你可以設置每頁顯示數(shù)據(jù)的大小,用DBQuery.shellBatchSize= 50;這樣每頁就顯示50條記錄了。

2、查詢?nèi)サ艉蟮漠斍熬奂现械哪沉械闹貜蛿?shù)據(jù)
db.userInfo.distinct("name");
會過濾掉name中的相同數(shù)據(jù)
相當于:select distict name from userInfo;

3、查詢age = 22的記錄
db.userInfo.find({"age": 22});
相當于: select * from userInfo where age = 22;

4、查詢age > 22的記錄
db.userInfo.find({age: {$gt: 22}});
相當于:select * from userInfo where age >22;

5、查詢age < 22的記錄
db.userInfo.find({age: {$lt: 22}});
相當于:select * from userInfo where age <22;

6、查詢age >= 25的記錄
db.userInfo.find({age: {$gte: 25}});
相當于:select * from userInfo where age >= 25;

7、查詢age <= 25的記錄
db.userInfo.find({age: {$lte: 25}});

8、查詢age >= 23 并且 age <= 26
db.userInfo.find({age: {$gte: 23, $lte: 26}});

9、查詢name中包含 mongo的數(shù)據(jù)
db.userInfo.find({name: /mongo/});
//相當于%%
[code]select * from userInfo where name like ‘%mongo%';

10、查詢name中以mongo開頭的
db.userInfo.find({name: /^mongo/});
select * from userInfo where name like ‘mongo%';

11、查詢指定列name、age數(shù)據(jù)
db.userInfo.find({}, {name: 1, age: 1});
相當于:select name, age from userInfo;
當然name也可以用true或false,當用ture的情況下河name:1效果一樣,如果用false就是排除name,顯示name以外的列信息。

12、查詢指定列name、age數(shù)據(jù), age > 25
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
相當于:select name, age from userInfo where age >25;

13、按照年齡排序
升序:db.userInfo.find().sort({age: 1});
降序:db.userInfo.find().sort({age: -1});

14、查詢name = zhangsan, age = 22的數(shù)據(jù)
db.userInfo.find({name: 'zhangsan', age: 22});
相當于:select * from userInfo where name = ‘zhangsan' and age = ‘22';

15、查詢前5條數(shù)據(jù)
db.userInfo.find().limit(5);
相當于:selecttop 5 * from userInfo;

16、查詢10條以后的數(shù)據(jù)
db.userInfo.find().skip(10);
相當于:select from userInfo where id not in (
selecttop 10
from userInfo
);

17、查詢在5-10之間的數(shù)據(jù)
db.userInfo.find().limit(10).skip(5);

可用于分頁,limit是pageSize,skip是第幾頁*pageSize

18、or與 查詢
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
相當于:select * from userInfo where age = 22 or age = 25;

19、查詢第一條數(shù)據(jù)
db.userInfo.findOne();
相當于:selecttop 1 * from userInfo;
db.userInfo.find().limit(1);

20、查詢某個結(jié)果集的記錄條數(shù)
db.userInfo.find({age: {$gte: 25}}).count();
相當于:select count(*) from userInfo where age >= 20;

21、按照某列進行排序
db.userInfo.find({sex: {$exists: true}}).count();
相當于:select count(sex) from userInfo;

五、索引
1、創(chuàng)建索引
db.userInfo.ensureIndex({name: 1});
db.userInfo.ensureIndex({name: 1, ts: -1});

2、查詢當前聚集集合所有索引
db.userInfo.getIndexes();

3、查看總索引記錄大小
db.userInfo.totalIndexSize();

4、讀取當前集合的所有index信息
db.users.reIndex();

5、刪除指定索引
db.users.dropIndex("name_1");

6、刪除所有索引索引
db.users.dropIndexes();

六、修改、添加、刪除集合數(shù)據(jù)
1、添加
db.users.save({name: ‘zhangsan', age: 25, sex: true});
添加的數(shù)據(jù)的數(shù)據(jù)列,沒有固定,根據(jù)添加的數(shù)據(jù)為準

2、修改
db.users.update({age: 25}, {$set: {name: 'changeName'}}, false, true);
相當于:update users set name = ‘changeName' where age = 25;
db.users.update({name: 'Lisi'}, {$inc: {age: 50}}, false, true);
相當于:update users set age = age + 50 where name = ‘Lisi';
db.users.update({name: 'Lisi'}, {$inc: {age: 50}, $set: {name: 'hoho'}}, false, true);
相當于:update users set age = age + 50, name = ‘hoho' where name = ‘Lisi';

3、刪除
db.users.remove({age: 132});

4、查詢修改刪除
db.users.findAndModify({
query: {age: {$gte: 25}},
sort: {age: -1},
update: {$set: {name: 'a2'}, $inc: {age: 2}},
remove: true
});
db.runCommand({ findandmodify : "users",
query: {age: {$gte: 25}},
sort: {age: -1},
update: {$set: {name: 'a2'}, $inc: {age: 2}},
remove: true
});

update 或 remove 其中一個是必須的參數(shù); 其他參數(shù)可選。
參數(shù) 詳解 默認值
query 查詢過濾條件 {}
sort 如果多個文檔符合查詢過濾條件,將以該參數(shù)指定的排列方式選擇出排在首位的對象,該對象將被操作 {}
remove 若為true,被選中對象將在返回前被刪除 N/A
update 一個 修改器對象
N/A
new 若為true,將返回修改后的對象而不是原始對象。在刪除操作中,該參數(shù)被忽略。 false
fields 參見Retrieving a Subset of Fields (1.5.0+)
All fields
upsert 創(chuàng)建新對象若查詢結(jié)果為空。 示例 (1.5.4+)
false

七、語句塊操作
1、簡單Hello World
print("Hello World!");
這種寫法調(diào)用了print函數(shù),和直接寫入"Hello World!"的效果是一樣的;

2、將一個對象轉(zhuǎn)換成json
tojson(new Object());
tojson(new Object('a'));

3、循環(huán)添加數(shù)據(jù)

for (var i = 0; i < 30; i++) {
... db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2});
... };

這樣就循環(huán)添加了30條數(shù)據(jù),同樣也可以省略括號的寫法

for (var i = 0; i < 30; i++) db.users.save({name: "u_" + i, age: 22 + i, sex: i % 2});

也是可以的,當你用db.users.find()查詢的時候,顯示多條數(shù)據(jù)而無法一頁顯示的情況下,可以用it查看下一頁的信息;

4、find 游標查詢

var cursor = db.users.find();
while (cursor.hasNext()) {
printjson(cursor.next());
}
這樣就查詢所有的users信息,同樣可以這樣寫
var cursor = db.users.find();
while (cursor.hasNext()) { printjson(cursor.next); }
同樣可以省略{}號

5、forEach迭代循環(huán)
db.users.find().forEach(printjson);
forEach中必須傳遞一個函數(shù)來處理每條迭代的數(shù)據(jù)信息

6、將find游標當數(shù)組處理
var cursor = db.users.find();
cursor[4];
取得下標索引為4的那條數(shù)據(jù)
既然可以當做數(shù)組處理,那么就可以獲得它的長度:cursor.length();或者cursor.count();
那樣我們也可以用循環(huán)顯示數(shù)據(jù)

for (var i = 0, len = c.length(); i < len; i++) printjson(c[i]);

7、將find游標轉(zhuǎn)換成數(shù)組

var arr = db.users.find().toArray();
printjson(arr[2]);

用toArray方法將其轉(zhuǎn)換為數(shù)組

8、定制我們自己的查詢結(jié)果
只顯示age <= 28的并且只顯示age這列數(shù)據(jù)
db.users.find({age: {$lte: 28}}, {age: 1}).forEach(printjson);
db.users.find({age: {$lte: 28}}, {age: true}).forEach(printjson);

排除age的列
db.users.find({age: {$lte: 28}}, {age: false}).forEach(printjson);

9、forEach傳遞函數(shù)顯示信息
db.things.find({x:4}).forEach(function(x) {print(tojson(x));});

八、其他
1、查詢之前的錯誤信息
db.getPrevError();

2、清除錯誤記錄
db.resetError();

查看聚集集合基本信息
1、查看幫助 db.yourColl.help();
2、查詢當前集合的數(shù)據(jù)條數(shù) db.yourColl.count();
3、查看數(shù)據(jù)空間大小 db.userInfo.dataSize();
4、得到當前聚集集合所在的db db.userInfo.getDB();
5、得到當前聚集的狀態(tài) db.userInfo.stats();
6、得到聚集集合總大小 db.userInfo.totalSize();
7、聚集集合儲存空間大小 db.userInfo.storageSize();
8、Shard版本信息 db.userInfo.getShardVersion()
9、聚集集合重命名 db.userInfo.renameCollection("users"); 將userInfo重命名為users
10、刪除當前聚集集合 db.userInfo.drop();

show dbs:顯示數(shù)據(jù)庫列表
show collections:顯示當前數(shù)據(jù)庫中的集合(類似關(guān)系數(shù)據(jù)庫中的表)
show users:顯示用戶
use <db name>:切換當前數(shù)據(jù)庫,這和MS-SQL里面的意思一樣
db.help():顯示數(shù)據(jù)庫操作命令,里面有很多的命令
db.foo.help():顯示集合操作命令,同樣有很多的命令,foo指的是當前數(shù)據(jù)庫下,一個叫foo的集合,并非真正意義上的命令
db.foo.find():對于當前數(shù)據(jù)庫中的foo集合進行數(shù)據(jù)查找(由于沒有條件,會列出所有數(shù)據(jù))
db.foo.find( { a : 1 } ):對于當前數(shù)據(jù)庫中的foo集合進行查找,條件是數(shù)據(jù)中有一個屬性叫a,且a的值為1

分享文章:Linux下的Mongodb部署應用梳理
文章來源:http://aaarwkj.com/article44/iijshe.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站導航服務器托管手機網(wǎng)站建設、小程序開發(fā)企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設計公司
国产精品国产亚洲精品看不| 亚洲人色中文字幕天堂| 青青草原在线视频观看| av免费在线不卡观看| 一区二区三区免费视频少妇| 国产亚洲精品热视频在线观看 | 欧美黄片一区二区三区三| 欧美一区日韩二区国产三区| 蜜臀综合亚洲国产精品| 激情五月开心五月麻豆| 最新国产av网址大全| 国产精品一区二区精品| 中文字幕熟妇人妻av在线| 亚洲成人av综合在线| 久久精品色一情一乱一伦| 少妇内射呻吟中文字幕视频| 日本一区二区高清网址| 亚洲精品国产第一区第二区| 久久精品一区二区三区乱码| 免费观看亚洲视频一区二区三区| 亚洲蜜桃精品一区二区三区| 日本岛国免费一区二区| 欧美日韩一区二区三区福利| 亚洲国产午夜精品不卡| 日本一区二区裸体视频| 涩涩涩丁香色婷五月网| 欧美日韩国产成人激情| 18禁成年大片免费网站| 野花日本免费高清完整| 蜜桃臀视频一区二区三区| 婷婷中文字幕在线不卡视频 | 久久伊人这里都是精品| 国产三级三级三级三级| 欧美日韩一区二区三区在线| 2023国产精品一区| 麻豆av永久地址久久精品| 欧美性大片一区二区三区| 久久人妻精品一区二区三区| 91好色视频在线观看| 少妇互射视频免费视频 | 精品久久精品久久人妻九色|