1. 程式人生 > >MongoDB Shell 常用操作命令

MongoDB Shell 常用操作命令

MonoDB   shell命令操作語法和JavaScript很類似,其實控制檯底層的查詢語句都是用JavaScript指令碼完成操作的。

Ø 資料庫

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();

三、Collection聚集集合操作

Ø 檢視聚集集合基本資訊

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);
相當於:select top 5 * from userInfo;
16、查詢10條以後的資料
db.userInfo.find().skip(10);
相當於:select * from userInfo where id not in (
select top 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();
相當於:select top 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({sex: {$exists: true}}).count();
相當於:select count(sex) 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, sex: 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

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, sex: i % 2});
... };
這樣就迴圈添加了30條資料,同樣也可以省略括號的寫法
> for (var i = 0; i < 30; i++) db.users.save({name: "u_" + i, age: 22 + i, sex: 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遊標當陣列處理
            
           

相關推薦

MongoDB Shell 常用操作命令

MonoDB   shell命令操作語法和JavaScript很類似,其實控制檯底層的查詢語句都是用JavaScript指令碼完成操作的。 Ø 資料庫 1、Help檢視命令提示 help db.help(); db.yourColl.help(); db.youCo

MongoDB資料庫常用操作命令

常用操作 檢視幫助 db.help() 當前資料庫的版本 db.version() 當前使用的資料庫 db.getName() 當前資料庫的狀態 db.stats() 當前資料庫的地址 db.getMongo() 查詢錯誤資訊

MongoDB常用操作命令

叠代 lai 運行 分頁 save 沒有 函數 foreach username MongoDB常用操作命令 如果你想創建一個“myTest”的數據庫,先運行use myTest命令,之後就做一些操作(如:db.createCollection(‘user‘)),這樣就可以

mongodb 常用操作命令

關閉mongodb > use admin; > db.shutdownServer(); 啟動     mongodb匯入匯出 ./mongoexport --host 127.0.0.1 --port 1001  --db z

MongoDB常用操作命令大全

閱讀目錄 插入文件 刪除文件 查詢文件 排序 索引 聚合 複製 回到頂部 建立、刪除資料庫 格式 use DATABASE_NAME 如果不存在,則建立,否則直接切換到該資料庫 顯示當前所在的資料庫 db 顯示所有資料庫 show

MongoDB的mongo shell常用操作方法及操作指令碼筆記

一、常用命令 1、Help檢視命令提示 ? 1 2 3 4 5 6 7 8 9 help db.help(); db.yourCol

MongoDB客戶端常用操作命令

> mongo   --進入shell介面以下命令是在mongo客戶端命令列下進行操作的> show dbs    -- 檢視資料庫列表> use admin   --建立admin資

MongoDB 常用操作命令

前言】         以下命令摘自官網截止目前2016年8月25日為止最新版3.2的部分,只作為參考,鄙人水平有限(其實我是渣渣)有的地方翻譯不到位,還請海涵並指出,一定虛心學習! 一、常用命令 命令 參考釋義

mongodb 常用操作命令大全1

1.查詢並且倒排序,限制顯示30條 db.getCollection('cars').find({'title':/appy/,'parentCard':0},{'title':1}).sort({'searchWeight':-1,'collectCount':-1,'c

mongodb shell常用命令,同樣適合於NoSqlL Manager for Mongodbshell

db.Eggs.find({Img:/115.28.183.79:8899/}).forEach(function(item){      db.Eggs.update({_id:item._id},{$set:{'Img':item.Img.replace("115.

mongodb常用操作命令整理

mongodb操作命令(注意所有db.開頭命令,請先使用use到指定db再操作) 連線操作資料庫: /usr/local/mongodb-3.0.4/bin/mongo 192.168.6.118:30000/admin 新增分片伺服器: db.runCommand({"

Mongdb學習(三)MongoDB常用操作命令大全

如果你想建立一個“myTest”的資料庫,先執行use myTest命令,之後就做一些操作(如:db.createCollection('user')),這樣就可以創 建一個名叫“myTest”的資料庫。 一、資料庫常用命令 1、Help檢視命令提示

redis的五大數據類型以及與 key 關鍵字相關的常用操作命令

redis數據類型 key關鍵字相關的指令 redis學習 1、redis的五大數據類型: 先來看看redis官方網上文檔 的介紹: 這裏簡單地說,就是redis不是一個普通的 key-value 存儲,而是一個數據結構服務器,支持各種不同 類型的值,這

linux下一些常用操作命令

linux 防火墻 端口 1、兩個服務器ssh免授權:ssh-keygen -t rsa 一直回車至結束ssh-copy-id -i /root/.ssh/id_rsa.pub 遠程ip 然後回車 輸入遠程ip密碼搞定,以後訪問就不用驗證密碼了2、壓縮和解壓縮命令: .tar

【Linux】Linux中常用操作命令

詳細信息 hadoop 用戶組 軟件 name vim使用 title redhat tail Linux簡介及Ubuntu安裝 常見指令 系統管理命令 打包壓縮相關命令 關機/重啟機器 Linux管道 Linux軟件包管理 vim使用 用戶及用戶組管理 文件權限管理

MongoDBMongoDB的一些操作命令

更新 use 使用 查看 count 一個 對象 分頁 ins 我們首先應該知道MongoDB的數據結構:MongoDB:庫-->集合-->JSON對象 查看 show dbs //查看有哪些庫 show collections //查看庫中有哪些

linux下svn的常用操作命令總結

svn版本管理導入代碼文件到 repo 版本庫我的代碼文件存放在 /var/www/html/; svn代碼版本庫的路徑 /application/svndata/repo (也就是剛剛我們創建的版本庫的位置);執行導入命令: svn import /var/www/html/svntest file:///

linux常用操作命令

bsp 文件內容 查找 remove move 分頁 force pwd 搜索 常用指令 ls   顯示文件或目錄(list) -l 列出文件詳細信息l(list) -a 列出當前目錄下所有文件及目錄,

git 常用操作命令

進行 歷史 指定 demo 新的 地址 克隆 保存 txt文件 記錄下工作中,常用的Git命令操作,一個項目經常是多人協作開發,使用好git是開發人員必備的技能,下面記錄下一些基本的git操作,後續會記錄git解決沖突,合並分支,回滾,tag操作等。 克隆項目 - git

Centos版Linux 一些常用操作命令

chmod passwd perm sea 發生 搜索命令 ted profile more Linux命令收集 1、文件處理命令:ls 功能描述:顯示目錄文件 命令英文原意:list 命令所在路徑:/bin/ls 執行權限:所有用戶 語法: ls 選項