MongoDB操作指令

1、mongo的基礎指令

show dbs 獲取你當前所有的數據庫
use dataBase_name 創建數據庫(沒有-創建/存在-使用)
db 指查詢你當前的數據庫
db.stats() 查詢你當前數據庫的狀態
db.dropDatabase() 刪除你當前的數據庫
db.help() 查詢幫助
db.version() 獲取你當前數據庫的版本
db.database_name(自己起的集合名字).help() 查詢任意數據庫的幫助
db.collection_name(自己起的集合名字).find() 查詢你當前集合內的信息
創建數據庫之後查看沒有出現music,
需要寫入一個文檔集合album;
insert插入 db.album.insertOne({‘title’:’xxxx’}),
查看採用db.album.find();
插入多條數據insertMany([{},{},{}])或者insert([{},{},{}])

2、Collection聚集集合操作
1、創建一個聚集集合
db.createCollection(“collName”, {size: 20, capped: true, max: 100});
db.collName.isCapped(); //判斷集合是否爲定容量
2、得到指定名稱的聚集集合
db.getCollection(“account”);
3、得到當前db的所有聚集集合
db.getCollectionNames();
4、顯示當前db所有聚集的狀態
db.printCollectionStats();
5、刪除集合
db.collectionname(集合名字).drop();
6、添加
db.users.save({name: ‘zhangsan’, age: 25, sex: true});
7、修改(四種方法)
所有數據都添加一個artist
(1)db.albums(集合名).updateMany({},{$set:{artist:‘哈哈’}})

(2)db.users(集合名).update({age: 25}, {KaTeX parse error: Expected 'EOF', got '}' at position 26: …: 'changeName'}}̲, false, true);…inc: {age: 50}}, false, true);
相當於:update users set age = age + 50 where name = ‘Lisi’;
(4)db.users(集合名).update({name: ‘Lisi’}, {$inc: {age: 50}, $set: {name: ‘hoho’}}, false, true);
相當於:update users set age = age + 50, name = ‘hoho’ where name = ‘Lisi’;
8、刪除
db.users(集合名).remove({age: 132});
9、查詢

(1)查詢所有記錄
db.userInfo(集合名).find();
相當於:select* from userInfo;
(2)查詢去重後數據
db.userInfo(集合名).distinct(“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: {KaTeX parse error: Expected 'EOF', got '}' at position 7: gt: 22}̲}); 相當於:select …lt: 22}});
相當於:select * from userInfo where age <22
(6)查詢age >= 25的記錄
db.userInfo(集合名).find({age: {KaTeX parse error: Expected 'EOF', got '}' at position 8: gte: 25}̲}); 相當於:select …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;
(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)限制數據量/幾條數據後
db.userInfo(集合名).find().limit(10).skip(5);
(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({sex: {$exists: true}}).count();
相當於:select count(sex) from userInfo;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章