佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > MongoDB常用數(shù)據(jù)庫命令大全

MongoDB常用數(shù)據(jù)庫命令大全

熱門標(biāo)簽:鐵路電話系統(tǒng) 地方門戶網(wǎng)站 呼叫中心市場需求 網(wǎng)站排名優(yōu)化 服務(wù)外包 AI電銷 百度競價(jià)排名 Linux服務(wù)器

一、MongoDB 數(shù)據(jù)庫常用操作命令

1、Help查看命令提示

help
db.help();
db.yourColl.help();

2、切換/創(chuàng)建數(shù)據(jù)庫

use raykaeso;

當(dāng)創(chuàng)建一個集合(table)的時(shí)候會自動創(chuàng)建當(dāng)前數(shù)據(jù)庫

3、查詢所有數(shù)據(jù)庫

show dbs;

4、刪除當(dāng)前使用數(shù)據(jù)庫

db.dropDatabase();

5、從指定主機(jī)上克隆數(shù)據(jù)庫

db.cloneDatabase(“127.0.0.1”);

將指定機(jī)器上的數(shù)據(jù)庫的數(shù)據(jù)克隆到當(dāng)前數(shù)據(jù)庫

6、從指定的機(jī)器上復(fù)制指定數(shù)據(jù)庫數(shù)據(jù)到某個數(shù)據(jù)庫

db.copyDatabase(“mydb”, “temp”, “127.0.0.1”);

將本機(jī)的mydb的數(shù)據(jù)復(fù)制到temp數(shù)據(jù)庫中

7、修復(fù)當(dāng)前數(shù)據(jù)庫

db.repairDatabase();

8、查看當(dāng)前使用的數(shù)據(jù)庫

db.getName()/db;

9、顯示當(dāng)前db狀態(tài)

db.stats();

10、當(dāng)前db版本

db.version();

11、查看當(dāng)前db的連接服務(wù)器機(jī)器地址

db.getMongo();

12、查詢之前的錯誤信息和清除

db.getPrevError();

db.resetError();

二、MongoDB Collection聚集集合

1、創(chuàng)建一個聚集集合(table)

db.createCollection(“collName”, {size: 20, capped: 5, max: 100});//創(chuàng)建成功會顯示{“ok”:1}

//判斷集合是否為定容量db.collName.isCapped();

2、得到指定名稱的聚集集合(table)

db.getCollection(“account”);

3、得到當(dāng)前db的所有聚集集合

db.getCollectionNames();

4、顯示當(dāng)前db所有聚集索引的狀態(tài)

db.printCollectionStats();

5、查詢當(dāng)前集合的數(shù)據(jù)條數(shù)

db.yourColl.count();

6、查看當(dāng)前集合數(shù)據(jù)空間大小

db.yourColl.dataSize();

7、得到當(dāng)前聚集集合所在的db

db.yourColl.getDB();

8、得到當(dāng)前聚集的狀態(tài)

db.coll.stats();

9、得到聚集集合總大小

db.coll.totalSize();

10、聚集集合儲存空間大小

db.coll.storageSize();

11、聚集集合重命名

db.coll.renameCollection(“ray”);

將coll重命名為ray

12、刪除當(dāng)前聚集集合

db.coll.drop();

三、MongoDB用戶相關(guān)

1、添加一個用戶(創(chuàng)建)

db.createUser({user: 'username', pwd: 'xxxx', roles: [{role: 'readWrite', db: 'dbname'}]});

添加用戶、設(shè)置密碼、是否只讀

2、數(shù)據(jù)庫認(rèn)證、安全模式(登錄)

db.auth(“ray”, “123456”);

3、顯示當(dāng)前所有用戶

show users;

4、刪除用戶

db.removeUser(“userName”);

四、MongoDB聚集集合查詢

1、查詢所有記錄

db.userInfo.find();

相當(dāng)于:select* from userInfo;

默認(rèn)每頁顯示20條記錄,當(dāng)顯示不下的情況下,可以用it迭代命令查詢下一頁數(shù)據(jù)。注意:鍵入it命令不能帶“;”

但是你可以設(shè)置每頁顯示數(shù)據(jù)的大小,用DBQuery.shellBatchSize= 50;這樣每頁就顯示50條記錄了。

2、查詢?nèi)サ艉蟮漠?dāng)前聚集集合中的某列的重復(fù)數(shù)據(jù)

db.userInfo.distinct(“name”);

會過濾掉name中的相同數(shù)據(jù)

相當(dāng)于:select distict name from userInfo;

3、查詢age = 22的記錄

db.userInfo.find({“age”: 22});

相當(dāng)于: select * from userInfo where age = 22;

4、條件查詢的記錄

MongoDB中條件操作符有:

(>) 大于 – $gt

() 小于 – $lt (>=) 大于等于 – $gte

(= ) 小于等于 – $lte

db.userInfo.find({age: {$gt: 22}}); 
相當(dāng)于:select * from userInfo where age>22;

db.userInfo.find({age: {$lt: 22}});
相當(dāng)于:select * from userInfo where age22; 

db.userInfo.find({age: {$gte: 25}}); 
相當(dāng)于:select * from userInfo where age >= 25;

6、字符模糊查詢

db.userInfo.find({name: /mongo/});
//相當(dāng)于%%

select * from userInfo where name like ‘%mongo%';

7、查詢指定列數(shù)據(jù)

db.userInfo.find({}, {name: 1, age: 1});

相當(dāng)于:select name, age from userInfo;

當(dāng)然name也可以用true或false

8、按條件查詢指定列數(shù)據(jù)

db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});

相當(dāng)于:select name, age from userInfo where age 25;

9、排序

升序:db.userInfo.find().sort({age: 1});

降序:db.userInfo.find().sort({age: -1});

10、查詢前5條數(shù)據(jù)

db.userInfo.find().limit(5);

相當(dāng)于:select * from userInfo limit 5;

11、查詢10條以后的數(shù)據(jù)

db.userInfo.find().skip(10);

相當(dāng)于:select count() from userInfo as total;

select from userInfo limit 10,total;

12、查詢在5-10之間的數(shù)據(jù)

db.userInfo.find().limit(10).skip(5);

可用于分頁,limit是pageSize,skip是第幾頁pageSize

相當(dāng)于:select from userInfo limit 5,10;

13、or與 查詢

db.userInfo.find({$or: [{age: 22}, {age: 25}]});

相當(dāng)于:select * from userInfo where age = 22 or age = 25;

14、查詢第一條數(shù)據(jù)

db.userInfo.findOne();

db.userInfo.find().limit(1);

相當(dāng)于:select * from userInfo limit 1;

15、查詢某個結(jié)果集的記錄條數(shù)

db.userInfo.find({age: {$gte: 25}}).count();

相當(dāng)于:select count(*) from userInfo where age >= 20;

五、MongoDB索引

1、創(chuàng)建索引

db.userInfo.ensureIndex({name: 1});

db.userInfo.ensureIndex({name: 1, ts: -1});

2、查詢當(dāng)前聚集集合所有索引

db.userInfo.getIndexes();

3、查看總索引記錄大小

db.userInfo.totalIndexSize();

4、讀取當(dāng)前集合的所有index信息

db.users.reIndex();

5、刪除指定索引

db.users.dropIndex(“name_1″);

6、刪除所有索引索引

db.users.dropIndexes();

六、MongoDB修改、添加、刪除集合數(shù)據(jù)

1、添加

db.users.save({name: ‘zhangsan', age: 25, sex: true});

添加的數(shù)據(jù)的數(shù)據(jù)列,沒有固定,根據(jù)添加的數(shù)據(jù)為準(zhǔn)

2、修改

db.users.update({age: 25}, {$set: {name: ‘changeName'}}, false, true);
相當(dāng)于:update users set name = ‘changeName' where age = 25;

db.users.update({name: ‘Lisi'}, {$inc: {age: 50}}, false, true);
相當(dāng)于:update users set age = age + 50 where name = ‘Lisi';

db.users.update({name: ‘Lisi'}, {$inc: {age: 50}, $set: {name: ‘hoho'}}, false, true);
相當(dāng)于: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: ‘a(chǎn)2′}, $inc: {age: 2}},
remove: true
});

更多關(guān)于MongoDB常用數(shù)據(jù)庫命令文章請查看下面的相關(guān)鏈接

您可能感興趣的文章:
  • Ubuntu下安裝PHP的mongodb擴(kuò)展操作命令
  • MongoDB常用操作命令大全
  • mongodb基本命令實(shí)例小結(jié)
  • MongoDB Shell 命令實(shí)例總結(jié)【進(jìn)階篇】
  • MongoDB使用mongoexport和mongoimport命令,批量導(dǎo)出和導(dǎo)入JSON數(shù)據(jù)到同一張表的實(shí)例
  • MongoDB基礎(chǔ)命令以及操作示例詳解
  • mongodb監(jiān)控工具mongostat的使用及命令詳解
  • Mongodb 啟動命令mongod參數(shù)說明(中文翻譯)
  • MongoDB使用自帶的命令行工具進(jìn)行備份和恢復(fù)的教程
  • MongoDB數(shù)據(jù)庫常用的10條操作命令

標(biāo)簽:蘭州 黃山 銅川 衡水 湖南 仙桃 湘潭 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MongoDB常用數(shù)據(jù)庫命令大全》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    梅州市| 定西市| 台安县| 阿克苏市| 高州市| 丹巴县| 清水县| 泌阳县| 绥德县| 福鼎市| 平湖市| 南和县| 永靖县| 通化市| 平果县| 富锦市| 华宁县| 治县。| 民丰县| 石城县| 普格县| 乌审旗| 山东省| 彰化市| 天津市| 大港区| 阳高县| 沙洋县| 桂东县| 杭州市| 上饶县| 渭源县| 齐齐哈尔市| 金阳县| 财经| 安塞县| 墨竹工卡县| 宾川县| 涞源县| 阿克陶县| 富平县|