摘要:如已存在數(shù)據,再次進行插入操作時,會報主鍵重復的錯誤提示會把修改為。使用函數(shù),參數(shù)指定查詢條件,不指定條件則查詢全部記錄。年齡為或者名字為,年齡在中,。
基本命令
顯示當前數(shù)據庫服務器上的數(shù)據庫
show dbs
切換到指定數(shù)據庫的上下文,可以在此上下文中管理testdb數(shù)據庫以及其中的集合等
use testdb
顯示數(shù)據庫中所有的集合(collection)
show collections
查看數(shù)據庫服務器的狀態(tài)
db.serverStatus()
查詢指定數(shù)據庫統(tǒng)計信息
use fragment
db.stats()
基本DDL和DML創(chuàng)建數(shù)據庫。
直接通過use dbname來切換到這個數(shù)據庫上下文下面,系統(tǒng)會自動延遲創(chuàng)建該數(shù)據庫。
use testdb show dbs (此刻可能testdb數(shù)據庫并沒有被創(chuàng)建) db (顯示當前使用的數(shù)據庫,結果位testdb) db.storeCollection.save({"version":"3.5", "segment":"e3ol6"}) (插入一條數(shù)據) show dbs (此刻能看到testdb被創(chuàng)建)
刪除數(shù)據庫。
直接使用db.dropDatabase()即可刪除數(shù)據庫。
創(chuàng)建集合。
db.createCollection("replicationColletion", {"capped":true, "size":10240, "max":17855200}) show collections
刪除集合。
db.mycoll.drop()
增加紀錄。
兩種插入方法
db.storeCollection.save({"version":"3.5", "segment":"e3ol6"})
db.storeCollection.insert({"version":"3.5", "segment":"e3ol6"})
若新增的數(shù)據中存在主鍵 ,insert() 會提示錯誤,而save() 則更改原來的內容為新內容。
如:
已存在數(shù)據: {_id : 1, " name " : " n1 " },再次進行插入操作時,
insert({_id : 1, " name " : " n2 " }) 會報主鍵重復的錯誤提示
save({ _id : 1, " name " : " n2 " }) 會把 n1 修改為 n2 。
相同點:
若新增的數(shù)據中沒有主鍵時,會增加一條記錄。
已存在數(shù)據: { _id : 1, " name " : " n1 " },再次進行插入操作時,
insert({ " name " : " n2 " }) 插入的數(shù)據因為沒有主鍵,所以會增加一條數(shù)據
save({ " name " : " n2 " }) 增加一條數(shù)據。
循環(huán)插入
for(var i = 1; i < 100; i++) {
db.testCollection.insert({ age : i % 7, name : "name" + Math.round((10 + 20 * Math.random())), password : "123456" })
}
更新記錄。
db.testCollection.update({age: 6}, {$inc: {age: 1}}) //選擇age為6的一條記錄,使他的age加1. db.testCollection.update({age: 7}, {$set: {password : "456789"}}) //選擇age為7的一條記錄,設置password為456789 //如果條件不匹配一個記錄,希望能往數(shù)據庫里新增一個,設置update函數(shù)第三個參數(shù)為true就可以。這里age為8條件匹配不到數(shù)據。 db.testCollection.update({age: 8}, {$set: {password : "888888"}}, true) //若要批量更新,設置update函數(shù)第四個參數(shù)為true就可以了。 db.testCollection.update({age: 7}, {$set: {password : "456789"}}, true, true)
更新version為3.5的記錄的segment值。
查詢一條紀錄。
db.storeCollection.findOne({"version":"3.5"})
參數(shù)為查詢條件,可選,系統(tǒng)會隨機查詢獲取到滿足條件的一條記錄(如果存在查詢結果數(shù)量大于等于1)。
查詢多條記錄。
db.storeCollection.find()
使用find()函數(shù),參數(shù)指定查詢條件,不指定條件則查詢全部記錄。
條件查詢包括4種方法:
// 1. $gt(>)、$gte(>=)、$lt(<)、$lt(<=)、$ne(!=)、沒有特殊關鍵字(=) db.testCollection.find({age: {$$gt:5}}).count() //14。note:去掉gt前面一個$符號,$符號會被解析,wiznote搞的鬼?。。? db.testCollection.find({age: {$gte:5}}).count() //28 db.testCollection.find({age: {$lt:5}}).count() //71 db.testCollection.find({age: {$lte:5}}).count() //85 db.testCollection.find({age: {$ne:5}}).count() //85 db.testCollection.find({age: 5}).count() //14 // 2. 沒有特殊關鍵字(&&)、 $or(||)、$in()、$nin() db.testCollection.find({age: {$$gt: 5}, password:"123456"}).count() //年齡大于5且密碼為123456, 14。 //年齡為5或者名字為name26,18 db.testCollection.find({$or:[{age: 5}, {name:"name26"}]}).count() db.testCollection.find({age: {$in: [1,5, 6]}}).count() //年齡在1、5、6中,43。 db.testCollection.find({age: {$nin: [1,5, 6]}}).count() //年齡不在1、5、6中,56 // 3. 正則表達式匹配,威力強勁 db.testCollection.find({name: /^name1d/}).count() //名字以name1開頭的,從name10到name19的記錄。41 //4. where語句,大招來了 db.testCollection.find({$where:function(){return this.age > 5} }).count()
刪除紀錄
db.storeCollection.findOne({"version": "3.5"}) (刪除version為3.5的紀錄) db.storeCollection.findOne({"version": "3.5"}) (返回結果為空)
統(tǒng)計集合記錄數(shù)
db.storeCollection.count()
查詢并統(tǒng)計結果記錄數(shù)
db.storeCollection.find({"segment":"e30l8"}).count()參考鏈接
> csdn
cnblogs
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://www.ezyhdfw.cn/yun/18836.html
摘要:二中常用命令顯示數(shù)據庫列表顯示當前數(shù)據庫中的集合類似關系數(shù)據庫中的表顯示用戶切換當前數(shù)據庫,如果數(shù)據庫不存在則創(chuàng)建數(shù)據庫。注意操作同時可以創(chuàng)建數(shù)據庫,如果一個不存在的數(shù)據庫名,則執(zhí)行后,會創(chuàng)建對應數(shù)據庫。如果指定字段,則會更新該的數(shù)據。 ..............................................................................
摘要:還原導出的表數(shù)據部分字段的表數(shù)據導入還原文件 一、 mongodump備份數(shù)據庫 1.一般常用的備份命令格式 mongodump -h IP --port 端口 -u 用戶名 -p 密碼 -d 數(shù)據庫 -o 文件存在路徑 如果想導出所有數(shù)據庫,可以去掉-d 2.導出數(shù)據庫[root@local ~]# mongodump -h 127.0.0.1 --port 30216 -d t...
摘要:既是數(shù)據庫,又是內存數(shù)據庫,而且它是現(xiàn)在最強大最流行的數(shù)據庫。所以相對于的真內存數(shù)據庫而言,只是將大部分的操作數(shù)據存在內存中。 MongoDB既是NoSQL數(shù)據庫,又是內存數(shù)據庫,而且它是現(xiàn)在最強大、最流行的NoSQL數(shù)據庫。區(qū)別與別的NoSQL數(shù)據庫,MongoDB主要是基于Documents文檔(即一條JSON數(shù)據)的。 MongoDB的特點: NoSQL數(shù)據庫 內存數(shù)據庫 存儲...
摘要:是的簡稱,是一個文檔對象的二進制編碼格式。有以下三個特點輕量級跨平臺效率高命名空間存儲對象到這一系列的數(shù)據庫名和名被稱為一個命名空間。 mongodb由C++寫就,其名字來自humongous這個單詞的中間部分,從名字可見其野心所在就是海量數(shù)據的處理。關于它的一個最簡潔描述為:scalable, high-performance, open source, schema-free, d...
閱讀 1539·2021-11-24 09:39
閱讀 1844·2021-11-22 15:25
閱讀 3813·2021-11-19 09:40
閱讀 3356·2021-09-22 15:31
閱讀 1370·2021-07-29 13:49
閱讀 1306·2019-08-26 11:59
閱讀 1379·2019-08-26 11:39
閱讀 985·2019-08-26 11:00