基本指令
mongo 進(jìn)入mongodb命令行
show dbs 顯示數(shù)據(jù)庫(kù)列表
use dbname 切換/創(chuàng)建dbname數(shù)據(jù)庫(kù),大小寫敏感
show collections 顯示數(shù)據(jù)庫(kù)中的集合
db.createCollection(‘users’) 創(chuàng)建users集合
db.users.drop()或db.runCommand({"drop","users"}) 刪除集合users
db.runCommand({"dropDatabase": 1}) 刪除當(dāng)前數(shù)據(jù)庫(kù)
Help 命令
odb.help();
odb.yourColl.help();
odb.youColl.find().help();
db.dropDatabase() 刪除當(dāng)前數(shù)據(jù)庫(kù)
db.cloneDatabase(“127.0.0.1”) 從指定主機(jī)上克隆數(shù)據(jù)庫(kù)
db.copyDatabase("mydb", "temp", "127.0.0.1") 從指定的機(jī)器上復(fù)制指定數(shù)據(jù)庫(kù)數(shù)據(jù)到某個(gè)數(shù)據(jù)庫(kù)捻悯,這里將本機(jī)的mydb的數(shù)據(jù)復(fù)制到temp數(shù)據(jù)庫(kù)中
db 查看當(dāng)前數(shù)據(jù)庫(kù)
db.version() 查看數(shù)據(jù)庫(kù)版本
db.getMongo() 查看當(dāng)前db的鏈接機(jī)器
增刪改查
創(chuàng)建&新增
-
save()
db.users.save({"name":"lecaf"}) 創(chuàng)建了名為users的集合异赫,并新增了一條{"name":"lecaf"}的數(shù)據(jù)
-
insert()
db.users.insert({"name":"ghost", "age":10}) 在users集合中插入一條新數(shù)據(jù),暇榴,如果沒有users這個(gè)集合蟀淮,mongodb會(huì)自動(dòng)創(chuàng)建
save()和insert()也存在著些許區(qū)別:若新增的數(shù)據(jù)主鍵已經(jīng)存在蚪腐,insert()會(huì)不做操作并提示錯(cuò)誤,而save() 則更改原來(lái)的內(nèi)容為新內(nèi)容侨糟。
存在數(shù)據(jù):{ _id : 1, " name " : " n1 "} 碍扔,_id是主鍵
insert({ _id : 1, " name " : " n2 " }) 會(huì)提示錯(cuò)誤
save({ _id : 1, " name " : " n2 " }) 會(huì)把 n1 改為 n2 ,有update的作用秕重。
刪除
db.users.remove() 刪除users集合下所有數(shù)據(jù)
db.users.remove({"name": "lecaf"}) 刪除users集合下name=lecaf的數(shù)據(jù)
查找
db.users.find() 查找users集合中所有數(shù)據(jù)
db.users.find({“name”:”feng”}) 查找users集合中name=feng的所有數(shù)據(jù)
db.users.findOne() 查找users集合中的第一條數(shù)據(jù)
db.users.find({“name”:”feng”}) 查找users集合中name=feng的數(shù)據(jù)集合中的第一條數(shù)據(jù)
修改
db.users.update({"name":"lecaf"}, {"age":10})
修改name=lecaf的數(shù)據(jù)為age=10蕴忆,第一個(gè)參數(shù)是查找條件,第二個(gè)參數(shù)是修改內(nèi)容悲幅,除了主鍵,其他內(nèi)容會(huì)被第二個(gè)參數(shù)的內(nèi)容替換站蝠,主鍵不能修改汰具,如圖
高級(jí)應(yīng)用
條件查找
db.collection.find({ "key" : value }) 查找key=value的數(shù)據(jù)
db.collection.find({ "key" : { $gt: value } }) key > value
db.collection.find({ "key" : { $lt: value } }) key < value
db.collection.find({ "key" : { $gte: value } }) key >= value
db.collection.find({ "key" : { $lte: value } }) key <= value
db.collection.find({ "key" : { $gt: value1 , $lt: value2 } }) value1 < key <value2
db.collection.find({ "key" : { $ne: value } }) key <> value
db.collection.find({ "key" : { $mod : [ 10 , 1 ] } }) 取模運(yùn)算,條件相當(dāng)于key % 10 == 1
db.collection.find({ "key" : { $nin: [ 1, 2, 3 ] } }) 不屬于集合中任何一個(gè)
db.collection.find({ "key" : { $in: [ 1, 2, 3 ] } }) 屬于中任何一個(gè)
db.collection.find({ "key" : { $size: 1 } }) $size 數(shù)量菱魔、尺寸留荔,條件相當(dāng)于key的值的數(shù)量是1(key必須是數(shù)組,一個(gè)值的情況不能算是數(shù)量為1的數(shù)組)
db.collection.find({ "key" : { $exists : true|false } }) $exists 字段存在澜倦,true返回存在字段key的數(shù)據(jù)聚蝶,false返回不存在字度key的數(shù)據(jù)
db.collection.find({ "key": /^val.*val$/i }) 字符串正則,類似like藻治;“i”忽略大小寫碘勉,“m”支持多行
db.collection.find({ $or : [{a : 1}, {b : 2} ] }) $or或 (注意:MongoDB 1.5.3后版本可用),符合條件a=1的或者符合條件b=2的數(shù)據(jù)都會(huì)查詢出來(lái)
db.collection.find({ "key": value , $or : [{ a : 1 } , { b : 2 }] }) 符合條件key=value 桩卵,同時(shí)符合其他兩個(gè)條件中任意一個(gè)的數(shù)據(jù)
db.collection.find({ "key.subkey" :value }) 內(nèi)嵌對(duì)象中的值匹配验靡,注意:"key.subkey"必須加引號(hào)
db.collection.find({ "key": { $not : /^val.*val$/i } }) 這是一個(gè)與其他查詢條件組合使用的操作符,不會(huì)單獨(dú)使用雏节。上述查詢條件得到的結(jié)果集加上$not之后就能獲得相反的集合胜嗓。
排序
db.collection.find({}).sort({ "key1" : -1 ,"key2" : 1 })
這里的1代表升序,-1代表降序
其他
db.collection.find({}).limit(5)
控制返回結(jié)果數(shù)量钩乍,如果參數(shù)是0辞州,則當(dāng)作沒有約束,limit()將不起作用(會(huì)對(duì)傳入?yún)?shù)求求絕對(duì)值)
db.collection.find({}).skip(5)
控制返回結(jié)果跳過(guò)多少數(shù)量寥粹,如果參數(shù)是0变过,則當(dāng)作沒有約束,skip()將不起作用排作,或者說(shuō)跳過(guò)了0條牵啦。(參數(shù)不能為負(fù)數(shù))可用于分頁(yè),limit是pageSize;skip 是第n頁(yè)*pageSize
db.collection.find({}).skip(5).limit(5)
可用來(lái)做分頁(yè)妄痪,跳過(guò)5條數(shù)據(jù)再取5條數(shù)據(jù)
db.collection.find({}).count(true)
count()返回結(jié)果集的條數(shù)
db.collection.find({}).skip(5).limit(5).count(true)
在加入skip()和limit()這兩個(gè)操作時(shí)哈雏,要獲得實(shí)際返回的結(jié)果數(shù),需要一個(gè)參數(shù)true,否則返回的是符合查詢條件的結(jié)果裳瘪,而不是數(shù)量
db.users.findAndModify({
query: {age: {$gte: 25}},
sort: {age: -1},
update: {
$set: {name: 'a2'},
$inc: {age: 2}
},
remove: true
});
組合查詢修改刪除