MongoDB資料庫的基本應用

2021-10-09 15:17:14 字數 4490 閱讀 4969

1、開啟 mongodb 服務:要管理資料庫,必須先開啟服務,開啟服務使用mongod --dbpath c:\mongodb

2、管理 mongodb 資料庫:mongo (一定要在新的 cmd 中輸入)

3、其他命令

清屏:cls

檢視所有資料庫列表:show dbs

1、使用資料庫、建立資料庫:use student

如果真的想把這個資料庫建立成功,那麼必須插入乙個資料。

資料庫中不能直接插入資料,只能往集合(collections)中插入資料。不需要專門建立集合,只需要寫點語法插入資料就會建立集合:

db.student.insert()

db.student 系統發現 student 是乙個陌生的集合名字,所以就自動建立了集合。

2、顯示當前的資料集合(mysql 中叫表):

show collections
3、刪除資料庫,刪除當前所在的資料庫:

db.dropdatabase()
4、刪除集合,刪除指定的集合 刪除表:

// 刪除集合 db.collection_name.drop()

db.user.drop()

插入資料,隨著資料的插入,資料庫建立成功了,集合也建立成功了。

db.表名.insert(); student 集合名稱(表)
1、查詢所有記錄

db.userinfo.find();

相當於:select* from userinfo;

2、查詢去掉後的當前聚集集合中的某列的重複資料

db.userinfo.distinct("name");

會過濾掉 name 中的相同資料

相當於:select distict name from userinfo;

3、查詢 age = 22 的記錄

db.userinfo.find();

相當於: select * from userinfo where age = 22;

4、查詢 age > 22 的記錄

db.userinfo.find(});

相當於:select * from userinfo where age >22;

5、查詢 age < 22 的記錄

db.userinfo.find(});

相當於:select * from userinfo where age <22;

6、查詢 age >= 25 的記錄

db.userinfo.find(});

相當於:select * from userinfo where age >= 25;

7、查詢 age <= 25 的記錄

db.userinfo.find(});
8、查詢 age >= 23 並且 age <= 26 注意書寫格式

db.userinfo.find(});
9、查詢 name 中包含 mongo 的資料 模糊查詢用於搜尋

db.userinfo.find();

//相當於%%

select * from userinfo where name like 『%mongo%』;

10、查詢 name 中以 mongo 開頭的

db.userinfo.find();

select * from userinfo where name like 『mongo%』;

11、查詢指定列 name、age 資料

db.userinfo.find({}, );

相當於:select name, age from userinfo;

當然 name 也可以用 true 或 false,當用 ture 的情況下和 name:1 效果一樣,如果用 false 就是排除 name,顯示 name 以外的列資訊。

12、查詢指定列 name、age 資料, age > 25

db.userinfo.find(}, );

相當於:select name, age from userinfo where age >25;

13、按照年齡排序 1 公升序 -1 降序

公升序:db.userinfo.find().sort();

降序:db.userinfo.find().sort();

14、查詢 name = zhangsan, age = 22 的資料

db.userinfo.find();

相當於:select * from userinfo where name = 『zhangsan』 and age = 『22』;

15、查詢前 5 條資料

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

相當於:selecttop 5 * from userinfo;

16、查詢 10 條以後的資料

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

相當於:select * from userinfo where id not in (selecttop 10 * from userinfo);

17、查詢在 5-10 之間的資料

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

可用於分頁,limit 是 pagesize,skip 是第幾頁*pagesize

18、or 與 查詢

db.userinfo.find(, ]});

相當於:select * from userinfo where age = 22 or age = 25;

19、findone 查詢第一條資料

db.userinfo.findone();

相當於:selecttop 1 * from userinfo;

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

20、查詢某個結果集的記錄條數 統計數量

db.userinfo.find(}).count();

相當於:select count(*) from userinfo where age >= 20;

如果要返回限制之後的記錄數量,要使用 count(true)或者 count(非 0)

db.users.find().skip(10).limit(5).count(true);

1、修改裡面還有查詢條件。你要改誰,要告訴 mongo。

查詢名字叫做小明的,把年齡更改為 16 歲:

db.student.update(,});
2、查詢數學成績是 70,把年齡更改為 33 歲:

db.student.update(,});
3、更改所有匹配專案:

db.student.update(,},);
完整替換,不出現$set 關鍵字了: 注意

db.student.update(,);
db.users.update(, }, false, true);

相當於:update users set age = age + 50 where name = 『lisi』;

db.users.update(, , $set: }, false, true);

相當於:update users set age = age + 50, name = 『hoho』 where name = 『lisi』;

db.collectionsnames.remove(  )
db.users.remove();

mongodb資料庫基本操作

一般來說,涉及到mongodb的操作主要有四種 增刪查改。nodejs可以很方便簡潔的實現這些操作。準備工作 連線mongodb伺服器 var server new mongodb.server localhost 27017,這裡server就指本地 localhost 的伺服器 連線伺服器上的資...

mongodb資料庫基本用法

常用命令 show dbs 顯示資料庫列表 show collections 顯示當前資料庫中的集合 類似關聯式資料庫中的表 show users 顯示使用者 usename 切換當前資料庫,這和ms sql裡面的意思一樣 db.help 顯示資料庫操作命令,裡面有很多的命令 db.foo help...

mongodb資料庫的基本使用

mongo使用其實很簡單 在寫如mongo資料庫前先要後台執行mongo 如下圖 2.引入from pymongo import mongoclient 3.建立mongodb物件 db mongoclient host 127.0.0.1 port 27017 注host填本機位址 4.獲取資料庫...