標籤:linux mongodb
Mongo DB ,是目前在IT行業非常流行的一種非關係型資料庫(NoSql),其靈活的資料存放區方式,備受當前IT從業人員的青睞。Mongo DB很好的實現了物件導向的思想(OO思想),在Mongo DB中 每一條記錄都是一個Document對象。Mongo DB最大的優勢在於所有的資料持久操作都無需開發人員手動編寫SQL語句,直接調用方法就可以輕鬆的實現CRUD操作。
一、下載mongodb
前往mongodb官網下載頁面:https://www.mongodb.org/downloads下載相應的版本,比如目前的Linux x64位最新版:mongodb-linux-x86_64-2.6.4.tgz
不過有點坑爹是,下載連結明明是tgz格式,結果下載後變成了gz格式:
mongodb1
先下載看看好了。
二、解壓mongodb
[[email protected] ~]# gzip -d mongodb-linux-x86_64-2.6.4.gz
得到的是mongodb-linux-x86_64-2.6.4,居然是個檔案,而不是檔案夾,和網上說的大相徑庭:
mongodb2
看來前面下載的確實存在問題!實際上應該是tgz檔案才對,按經驗分析了一下,下載的實際上還是tgz檔案,顯示為gz檔案只是形式上的假象!所以,gzip只解壓了壓縮包的外層,實際上還需要解壓一層tar存檔屬性!
於是,先將解壓後的檔案重新命名加上tar格式:
[[email protected] ~]# mv mongodb-linux-x86_64-2.6.4 mongodb-linux-x86_64-2.6.4.tar
然後,使用tar解壓即可:
?1 [[email protected] ~]# tar xvf mongodb-linux-x86_64-2.6.4.tar
將解壓後的檔案夾移動&重新命名至/usr/local/mongodb
[[email protected] ~]# mv mongodb-linux-x86_64-2.6.4 /usr/local/mongodb [[email protected] ~]# cd /usr/local/mongodb/bin/ [[email protected] ~]# ll
bin下的mongod就是MongoDB的服務端進程,mongo就是其用戶端,其它的命令用於MongoDB的其它用途如MongoDB檔案匯出等。
三、啟動mongodb
啟動前,先指定mongodb的data目錄,如果沒有就建立一個:
[[email protected] ~]# cd /usr/local/mongodb [[email protected] mongodb]# mkdir data
然後,執行如下命令即可啟動mongodb:
[[email protected] mongodb]# /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/data/mongodb.log --logappend&
mongodb5
啟動成功後,可查看是否啟動成功了,預設連接埠號碼是27017,當然在啟動時也可以指定未使用的其它連接埠。
mongodb6
最後,將用戶端mogo檔案在/bin下軟連結,方便隨處執行:
ln -s /usr/local/mongodb/bin/mongo /bin/mongo
現在使用mongo用戶端訪問一下該資料庫:
[[email protected] bin]# ./mongo MongoDB shell version: 2.6.4 connecting to: test >
安裝成功!
四、附:基本操作
MongoDB資料庫基本用法 show dbs:顯示資料庫列表 show collections:顯示當前資料庫中的集合(類似關聯式資料庫中的表) show users:顯示使用者 use <db name>:切換當前資料庫,這和MS-SQL裡面的意思一樣 db.help():顯示資料庫操作命令,裡面有很多的命令 db.foo.help():顯示集合操作命令,同樣有很多的命令,foo指的是當前資料庫下,一個叫foo的集合,並非真正意義上的命令 db.foo.find():對於當前資料庫中的foo集合進行資料尋找(由於沒有條件,會列出所有資料) db.foo.find( { a : 1 } ):對於當前資料庫中的foo集合進行尋找,條件是資料中有一個屬性叫a,且a的值為1 MongoDB沒有建立資料庫的命令,但有類似的命令。 如:如果你想建立一個“myTest”的資料庫,先運行use myTest命令,之後就做一些操作(如:db.createCollection(‘user‘)),這樣就可以建立一個名叫“myTest”的資料庫。 資料庫常用命令 1、Help查看命令提示 help db.help(); db.yourColl.help(); db.youColl.find().help(); rs.help(); 2、切換/建立資料庫 use yourDB; 當建立一個集合(table)的時候會自動建立當前資料庫 3、查詢所有資料庫 show dbs; 4、刪除當前使用資料庫 db.dropDatabase(); 5、從指定主機上複製資料庫 db.cloneDatabase(“127.0.0.1”); 將指定機器上的資料庫的資料複製到當前資料庫 6、從指定的機器上複製指定資料庫資料到某個資料庫 db.copyDatabase("mydb", "temp", "127.0.0.1");將原生mydb的資料複製到temp資料庫中 7、修複當前資料庫 db.repairDatabase(); 8、查看當前使用的資料庫 db.getName(); db; db和getName方法是一樣的效果,都可以查詢當前使用的資料庫 9、顯示當前db狀態 db.stats(); 10、當前db版本 db.version(); 11、查看當前db的連結機器地址 db.getMongo(); Collection聚集集合 1、建立一個聚集集合(table) db.createCollection(“collName”, {size: 20, capped: 5, max: 100}); 2、得到指定名稱的聚集集合(table) db.getCollection("account"); 3、得到當前db的所有聚集集合 db.getCollectionNames(); 4、顯示當前db所有叢集索引的狀態 db.printCollectionStats(); 使用者相關 1、添加一個使用者 db.addUser("name"); db.addUser("userName", "pwd123", true); 添加使用者、設定密碼、是否唯讀 2、資料庫認證、安全模式 db.auth("userName", "123123"); 3、顯示當前所有使用者 show users; 4、刪除使用者 db.removeUser("userName"); 其他 1、查詢之前的錯誤資訊 db.getPrevError(); 2、清除錯誤記錄 db.resetError(); 查看聚集集合基本資料 1、查看協助 db.yourColl.help(); 2、查詢當前集合的資料條數 db.yourColl.count(); 3、查看資料空間大小 db.userInfo.dataSize(); 4、得到當前聚集集合所在的db db.userInfo.getDB(); 5、得到當前聚集的狀態 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(); 聚集集合查詢 1、查詢所有記錄 db.userInfo.find(); 相當於:select* from userInfo; 預設每頁顯示20條記錄,當顯示不下的情況下,可以用it迭代命令查詢下一頁資料。注意:鍵入it命令不能帶“;” 但是你可以設定每頁顯示資料的大小,用DBQuery.shellBatchSize= 50;這樣每頁就顯示50條記錄了。 2、查詢去掉後的當前聚集集合中的某列的重複資料 db.userInfo.distinct("name"); 會過濾掉name中的相同資料 相當於: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的資料 db.userInfo.find({name: /mongo/}); //相當於%% 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資料 db.userInfo.find({}, {name: 1, age: 1}); 相當於:select name, age from userInfo; 當然name也可以用true或false,當用ture的情況下河name:1效果一樣,如果用false就是排除name,顯示name以外的列資訊。 12、查詢指定列name、age資料, 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的資料 db.userInfo.find({name: ‘zhangsan‘, age: 22}); 相當於:select * from userInfo where name = ‘zhangsan‘ and age = ‘22‘; 15、查詢前5條資料 db.userInfo.find().limit(5); 相當於:selecttop 5 * from userInfo; 16、查詢10條以後的資料 db.userInfo.find().skip(10); 相當於:select * from userInfo where id not in ( selecttop 10 * from userInfo ); 17、查詢在5-10之間的資料 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、查詢第一條資料 db.userInfo.findOne(); 相當於:selecttop 1 * from userInfo; db.userInfo.find().limit(1); 20、查詢某個結果集的記錄條數 db.userInfo.find({age: {$gte: 25}}).count(); 相當於:select count(*) from userInfo where age >= 20; 21、按照某列進行排序 db.userInfo.find({***: {$exists: true}}).count(); 相當於:select count(***) from userInfo; 索引 1、建立索引 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(); 修改、添加、刪除集合資料 1、添加 db.users.save({name: ‘zhangsan‘, age: 25, ***: true}); 添加的資料的資料列,沒有固定,根據添加的資料為準 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 其中一個是必須的參數; 其他參數可選。 參數 詳解 預設值 query 查詢過濾條件 {} sort 如果多個文檔符合查詢過濾條件,將以該參數指定的相片順序選擇出排在首位的對象,該對象將被操作 {} remove 若為true,被選中對象將在返回前被刪除 N/A update 一個 修改器對象 N/A new 若為true,將返回修改後的對象而不是原始對象。在刪除操作中,該參數被忽略。 false fields 參見Retrieving a Subset of Fields (1.5.0+) All fields upsert 建立新對象若查詢結果為空白。 樣本 (1.5.4+) false 語句塊操作 1、簡單Hello World print("Hello World!"); 這種寫法調用了print函數,和直接寫入"Hello World!"的效果是一樣的; 2、將一個對象轉換成json tojson(new Object()); tojson(new Object(‘a‘)); 3、迴圈添加資料 > for (var i = 0; i < 30; i++) { ... db.users.save({name: "u_" + i, age: 22 + i, ***: i % 2}); ... }; 這樣就迴圈添加了30條資料,同樣也可以省略括弧的寫法 > for (var i = 0; i < 30; i++) db.users.save({name: "u_" + i, age: 22 + i, ***: i % 2}); 也是可以的,當你用db.users.find()查詢的時候,顯示多條資料而無法一頁顯示的情況下,可以用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迭代迴圈 db.users.find().forEach(printjson); forEach中必須傳遞一個函數來處理每條迭代的資料資訊 6、將find遊標當數組處理 var cursor = db.users.find(); cursor[4]; 取得下標索引為4的那條資料 既然可以當做數組處理,那麼就可以獲得它的長度:cursor.length();或者cursor.count(); 那樣我們也可以用迴圈顯示資料 for (var i = 0, len = c.length(); i < len; i++) printjson(c[i]); 7、將find遊標轉換成數組 > var arr = db.users.find().toArray(); > printjson(arr[2]); 用toArray方法將其轉換為數組 8、定製我們自己的查詢結果 只顯示age <= 28的並且只顯示age這列資料 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傳遞函數顯示資訊 db.things.find({x:4}).forEach(function(x) {print(tojson(x));});
Linux下MongoDB的安裝和操作