一、MongoDB 數(shù)據(jù)庫(kù)常用操作命令 1、Help查看命令提示 help db.help(); db.yourColl.help(); 2、切換/創(chuàng)建數(shù)據(jù)庫(kù) use raykaeso; 當(dāng)創(chuàng)建一個(gè)集合(table)的時(shí)候會(huì)自動(dòng)創(chuàng)建當(dāng)前數(shù)據(jù)庫(kù)
3、查詢所有數(shù)據(jù)庫(kù) show dbs; 4、刪除當(dāng)前使用數(shù)據(jù)庫(kù) db.dropDatabase(); 5、從指定主機(jī)上克隆數(shù)據(jù)庫(kù) db.cloneDatabase(“127.0.0.1”); 將指定機(jī)器上的數(shù)據(jù)庫(kù)的數(shù)據(jù)克隆到當(dāng)前數(shù)據(jù)庫(kù)
2、數(shù)據(jù)庫(kù)認(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)每頁(yè)顯示20條記錄,當(dāng)顯示不下的情況下,可以用it迭代命令查詢下一頁(yè)數(shù)據(jù)。注意:鍵入it命令不能帶“;”,但是你可以設(shè)置每頁(yè)顯示數(shù)據(jù)的大小,用DBQuery.shellBatchSize= 50;這樣每頁(yè)就顯示50條記錄了。
2、查詢?nèi)サ艉蟮漠?dāng)前聚集集合中的某列的重復(fù)數(shù)據(jù) db.userInfo.distinct(“name”); 會(huì)過(guò)濾掉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中條件操作符有:
(>) 大于 -C $gt
(<) 小于 -C $lt (>=) 大于等于 -C $gte
(<= ) 小于等于 -C $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 age<22;
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