在客戶端指定數(shù)據(jù)庫(kù)進(jìn)行連接:(默認(rèn)連接本機(jī)test數(shù)據(jù)庫(kù))
[mongod@MongoDB ~]$ mongo10.0.0.152/admin
MongoDB shell version: 3.2.8
connecting to: 10.0.0.152/admin
> db
admin
查看當(dāng)前數(shù)據(jù)庫(kù)版本
> db.version()
3.2.8
切換數(shù)據(jù)庫(kù)
> use test;
switched to db test
顯示當(dāng)前數(shù)據(jù)庫(kù)
> db
test
> db.getName()
test
查詢所有數(shù)據(jù)庫(kù)
> show dbs;
clsn? 0.000GB
local? 0.000GB
test? 0.000GB
> show databases;
clsn? 0.000GB
local? 0.000GB
test? 0.000GB
查看clsn數(shù)據(jù)庫(kù)當(dāng)前狀態(tài)
>use clsn;
> db.stats()
{
? ? "db":"clsn",
? ? "collections":1,
? ? "objects":10000,
? ? "avgObjSize":80,
? ? "dataSize":800000,
? ? "storageSize":258048,
? ? "numExtents":0,
? ? "indexes":1,
? ? "indexSize":94208,
? ? "ok":1}
查看當(dāng)前數(shù)據(jù)庫(kù)的連接機(jī)器地址
> db.getMongo()
connection to 127.0.0.1
1.5.2 數(shù)據(jù)管理
創(chuàng)建數(shù)據(jù)庫(kù)
> use clsn;
說明:
創(chuàng)建數(shù)據(jù)庫(kù):
當(dāng)use的時(shí)候,系統(tǒng)就會(huì)自動(dòng)創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)。
如果use之后沒有創(chuàng)建任何集合。系統(tǒng)就會(huì)刪除這個(gè)數(shù)據(jù)庫(kù)。
刪除數(shù)據(jù)庫(kù)
> show dbs;
clsn? 0.000GB
local? 0.000GB
test? 0.000GB
> use clsn
switched to db clsn
> db.dropDatabase()
{ "dropped" : "clsn", "ok" : 1 }
說明:
刪除數(shù)據(jù)庫(kù):
如果沒有選擇任何數(shù)據(jù)庫(kù),會(huì)刪除默認(rèn)的test數(shù)據(jù)庫(kù)
創(chuàng)建集合
?? 方法一:
> use clsn;
switched to db clsn
> db.createCollection('a')
{ "ok" : 1 }
> db.createCollection('b')
{ "ok" : 1 }
?? 查看當(dāng)前數(shù)據(jù)下的所有集合
> show collections;
a
b
> db.getCollectionNames()
[ "a","b"]
方法二:
當(dāng)插入一個(gè)文檔的時(shí)候,一個(gè)集合就會(huì)自動(dòng)創(chuàng)建。
>use clsn;
switched to db clsn
> db.c.insert({name:'clsn'});
WriteResult({ "nInserted":1 })
> db.c.insert({url:'http://blog.nmtui.com'});
WriteResult({ "nInserted":1})
?? 查看創(chuàng)建的合集
> db.getCollectionNames()
[ "a","b","c"]
?? 查看合集里的內(nèi)容
> db.c.find()
{ "_id": ObjectId("5a4cbcea83ec78b7bea904f8"),"name":"clsn" }
{ "_id": ObjectId("5a4cbcfc83ec78b7bea904f9"),"url":"http://blog.nmtui.com"}
重命名集合
> db.c.renameCollection("clsn")
{ "ok":1 }
> db.getCollectionNames()
[ "a","b","clsn"]
?? 刪除合集
> db.a.drop()true
> db.getCollectionNames()
[ "b","clsn"]
?? 插入1w行數(shù)據(jù)
>for(i=0;i<10000;i++){ db.log.insert({"uid":i,"name":"mongodb","age":6,"date":new Date()}); }
WriteResult({ "nInserted":1})
查詢集合中的查詢所有記錄
> db.log.find()
注:默認(rèn)每頁(yè)顯示20條記錄,當(dāng)顯示不下的的情況下,可以用it迭代命令查詢下一頁(yè)數(shù)據(jù)。
> DBQuery.shellBatchSize=50;? ? # 每頁(yè)顯示50條記錄50
app
> db.log.findOne()? ? ? ? ? ? # 查看第1條記錄
app
> db.log.count()? ? ? ? ? ? ? # 查詢總的記錄數(shù)
app
> db.log.find({uid:1000});? ? # 查詢UUID為1000的數(shù)據(jù)
刪除集合中的記錄數(shù)
>? db.log.distinct("name")? ? ? #? 查詢?nèi)サ舢?dāng)前集合中某列的重復(fù)數(shù)據(jù)
[ "mongodb" ]
> db.log.remove({})? ? ? ? ? ? #? 刪除集合中所有記錄
WriteResult({ "nRemoved":10000 })??
> db.log.distinct("name")
[ ]
查看集合存儲(chǔ)信息
> db.log.stats()? ? ? ? ? # 查看數(shù)據(jù)狀態(tài)
> db.log.dataSize()? ? ? # 集合中數(shù)據(jù)的原始大小> db.log.totalIndexSize() # 集合中索引數(shù)據(jù)的原始大小> db.log.totalSize()? ? ? # 集合中索引+數(shù)據(jù)壓縮存儲(chǔ)之后的大小> db.log.storageSize()? ? # 集合中數(shù)據(jù)壓縮存儲(chǔ)的大小
pretty()使用
> db.log.find({uid:1000}).pretty()
{
? ? "_id": ObjectId("5a4c5c0bdf067ab57602f7c2"),
? ? "uid":1000,
? ? "name":"mongodb",
? ? "age":6,
? ? "date": ISODate("2018-01-03T04:28:59.343Z")
}

"show dbs"?命令可以顯示所有數(shù)據(jù)的列表。
執(zhí)行?"db"?命令可以顯示當(dāng)前數(shù)據(jù)庫(kù)對(duì)象或集合。
運(yùn)行"use"命令,可以連接到一個(gè)指定的數(shù)據(jù)庫(kù)。


刪除數(shù)據(jù)庫(kù):
use runoob
db.dropDatabase()
刪除集合:db.collection.drop()
> use runoob
switched to db runoob
> show tables
site
> db.site.drop()
true
> show tables
創(chuàng)建集合:db.createCollection(name, options)
use test
db.createCollection("runoob")
db.createCollection("mycol", { capped : true, autoIndexId : true, size :? ? 6142800, max : 10000 } )
在 MongoDB 中,你不需要?jiǎng)?chuàng)建集合。當(dāng)你插入一些文檔時(shí),MongoDB 會(huì)自動(dòng)創(chuàng)建集合。
> db.mycol2.insert({"name" : "菜鳥教程"})
刪除集合:
db.collection.drop()
>use mydb
switched to db mydb
>show collections
mycol
mycol2
system.indexes
runoob
>db.mycol2.drop()
true
插入文檔:db.COLLECTION_NAME.insert(document)
>db.col.insert({title: 'MongoDB 教程',? description: 'MongoDB 是一個(gè) Nosql 數(shù)據(jù)庫(kù)',? ? by: '菜鳥教程',url:'http://www.runoob.com',? ? tags: ['mongodb', 'database', 'NoSQL'],? ? likes: 100})
查看已插入文檔:
> db.col.find()
更新文檔:
update() 方法用于更新已存在的文檔。語(yǔ)法格式如下:
db.collection.update(? <query>,? <update>,? {? ? upsert: <boolean>,? ? multi: <boolean>,? ? writeConcern: <document>? })
參數(shù)說明:
query?: update的查詢條件,類似sql update查詢內(nèi)where后面的。
update?: update的對(duì)象和一些更新的操作符(如$,$inc...)等,也可以理解為sql update查詢內(nèi)set后面的
upsert?: 可選,這個(gè)參數(shù)的意思是,如果不存在update的記錄,是否插入objNew,true為插入,默認(rèn)是false,不插入。
multi?: 可選,mongodb 默認(rèn)是false,只更新找到的第一條記錄,如果這個(gè)參數(shù)為true,就把按條件查出來多條記錄全部更新。
writeConcern?:可選,拋出異常的級(jí)別。
db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}})
save() 方法:
save() 方法通過傳入的文檔來替換已有文檔。語(yǔ)法格式如下:
db.collection.save(? <document>,? {? ? writeConcern: <document>? })
參數(shù)說明:
document?: 文檔數(shù)據(jù)。
writeConcern?:可選,拋出異常的級(jí)別。
>db.col.save({????"_id" : ObjectId("56064f89ade2f21f36b03136"), "title" : "MongoDB", "description" : "MongoDB 是一個(gè) Nosql 數(shù)據(jù)庫(kù)", "by" : "Runoob", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "NoSQL" ], "likes" : 110})
刪除文檔:
db.collection.remove( <query>, { justOne: <boolean>, writeConcern: <document> })
參數(shù)說明:
query?:(可選)刪除的文檔的條件。
justOne?: (可選)如果設(shè)為 true 或 1,則只刪除一個(gè)文檔。
writeConcern?:(可選)拋出異常的級(jí)別。
>db.col.remove({'title':'MongoDB 教程'})
查詢文檔:db.collection.find(query, projection)
query?:可選,使用查詢操作符指定查詢條件
projection?:可選,使用投影操作符指定返回的鍵。查詢時(shí)返回文檔中所有鍵值, 只需省略該參數(shù)即可(默認(rèn)省略)。
如果你需要以易讀的方式來讀取數(shù)據(jù),可以使用 pretty() 方法,語(yǔ)法格式如下:
>db.col.find().pretty()
pretty() 方法以格式化的方式來顯示所有文檔。
db.col.find().pretty()

and:>db.col.find({key1:value1, key2:value2}).pretty()
> db.col.find({"by":"菜鳥教程", "title":"MongoDB 教程"}).pretty()
or:>db.col.find( { $or: [???? {key1: value1}, {key2:value2} ] }).pretty()
>db.col.find({$or:[{"by":"菜鳥教程"},{"title": "MongoDB 教程"}]}).pretty()
AND 和 OR 聯(lián)合使用:
>db.col.find({"likes": {$gt:50}, $or: [{"by": "菜鳥教程"},{"title": "MongoDB 教程"}]}).pretty()