mongo命令
基本驗證
數字運算1+2
show dbs;顯示數據庫
use 數據庫名;使用某個數據庫;
db.dropDatabase();刪除當前數據庫
db.getName();查看當前數據庫
db.stats(); 查看當前狀態
db.getMongo(); 查看當前連接地址
db.getPrevError();查看錯誤信息
db.resetError();刪除錯誤信息
db.addUser("name");db.addUser("userName", "pwd123", true);添加用戶、設置密碼、是否只讀
db.auth("userName", "123123");數據庫認證、安全模式 結果1表示正確,
show users;顯示當前所有用戶show users;
db.removeUser("userName");刪除用戶
db.createCollection(“collName”, {size: 20, capped: 5, max: 100});創建固定集合,不可刪除記錄,查詢優化
db.createCollection("testTow",{size:10,max:50}); 創建一個普通集合。可刪除記錄
db.getCollection("account");查看集合
db.getCollectionNames();顯示當前數據庫的所有集合
db.printCollectionStats();顯示集合結構信息
集合本身屬性操作
db.集合.help()對這個集合可以的操作
db.集合.count()集合記錄個數
db.集合.dataSize();集合所佔文件大小
db.集合.getDB()集合在哪個數據庫實體中
db.集合.stats()集合的結構信息
db.userInfo.totalSize();得到聚集集合總大小
db.userInfo.storageSize();聚集集合儲存空間大小
db.userInfo.getShardVersion()Shard版本信息
db.userInfo.renameCollection("users");集合重命名
db.userInfo.drop();刪除集合
集合中的記錄操作
db.集合.save({鍵值對格式,逗號分割}) 集合插入記錄
db.集合.find({鍵值對格式,多條件逗號分割})查詢記錄
db.集合.update(查詢鍵值對,多條件逗號分割),{$set:{修改值鍵值對}}修改記錄
db.集合.remove(鍵值對)刪除記錄
db.集合.remove()刪除所有記錄
查詢修改刪除
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});
集合記錄查詢
db.集合.find()查詢所有
db.集合.distinct("name");查詢結果去除某列重複的
db.userInfo.find({"age": 22});按條件查詢
db.userInfo.find({age: {$gt: 22}});>22
db.userInfo.find({age: {$lt: 22}});<22
db.userInfo.find({age: {$gte: 25}});>=
db.userInfo.find({age: {$lte: 25}});<=
db.userInfo.find({age: {$gte: 23, $lte: 26}});>= <=
db.userInfo.find({name: /mongo/});包含%mongo%
db.userInfo.find({name: /^mongo/});包含mongo%
db.userInfo.find({}, {name: 1, age: 1});查詢結果是name 和age量列的所有值
=db.集合.find({},{name:true,age:true,sex:false});
db.userInfo.find().sort({age: 1});升序
db.userInfo.find().sort({age: -1});降序
db.userInfo.find().limit(5);查詢前5條記錄
db.userInfo.find().skip(10);查詢10條後的記錄
db.userInfo.find().limit(10).skip(5);查詢5-10的記錄
db.userInfo.find({$or: [{age: 22}, {age: 25}]});or 查詢
db.userInfo.find({sex: {$exists: true}}).count();查詢是否存在sex值是true的記錄,並計數
索引
db.userInfo.ensureIndex({name: 1});db.userInfo.ensureIndex({name: 1, ts: -1});
db.userInfo.getIndexes();
db.userInfo.totalIndexSize();
db.users.reIndex();
db.users.dropIndex("name_1");
db.users.dropIndexes();
語句塊操作
基本運算:1+1
字符串連接 "h"+"s";
...
print(要打印的對象)
tojson(new Object('a'));將一個對象轉成json對象
for循環,js格式
for(var i=0;i<30;i++){
print(i);
};
遊標緩存查詢結果
var cursor = db.users.find();
while (cursor.hasNext()) {
printjson(cursor.next());
}將查詢結果按照json格式顯示
或者
var cursor = db.users.find();
for (var i = 0, len = c.length(); i < len; i++) printjson(c[i]);
數組
var arr = db.users.find().toArray();
printjson(arr[2]);
將查詢結果安裝json格式打印
db.users.find({age: {$lte: 28}}, {age: true}).forEach(printjson);
帶參數傳遞的查詢
db.things.find({x:4}).forEach(function(x) {print(tojson(x));});
導入導出
新起cmd窗口。錄入mongoexport,進入mongoexport命令窗口。不能原來的mongo的窗口操作。
-h:指明數據庫宿主機的IP
-u:指明數據庫的用戶名
-p:指明數據庫的密碼
-d:指明數據庫的名字
-c:指明collection的名字
-f:指明要導出那些列
-o:指明到要導出的文件名
-q:指明導出數據的過濾條件
-csv:指明導出文件格式csv
默認json
mongoexport
./bin/mongoexport -d test -c students -o [路徑]students.dat
導出test數據的students集合到當前路徑下的students.dat文件,json格式。
./bin/mongoexport -d test -c students --csv -f classid,name,age -o students_csv.dat
mongoexport -d test -c testOne --csv -f name,age -o f:/testOne.csv
mongoimport
./bin/mongoimport -d test -c students students.dat
./bin/mongoimport -d test -c students --type csv --headerline --file students_csv.dat
--headerline 表示第一行是文件頭,不要導入,否則會報異常
$(date +%F)爲shell命令,會輸出當前日期,格式爲2012-02-22
mongoexport -d search_logs -c key_words -q '{request_time:{$gte:new Date(1329490800000)}}' -o mongo_$(date +%F).json輸出帶日期的文件
mongo特性
固定集合與普通集合
capped Collection:性能好,能查詢,更新,插入。不能刪除,文件大小固定,超出大小則不能插入和更新。
能夠通過drop刪除所有。
創建方式
db.createCollection("mycoll", {capped:true, size:100000, max:100});
查看是否是固定集合
db.集合.isCapped();true是
db.集合.validate();1表示可以插入文檔
普通集合
db.runCommand({convertToCapped:'mycoll',size:10000,max:3})將一個普通的集合轉爲固定集合