mongdb資料庫的操作

2022-06-02 18:45:11 字數 2774 閱讀 4870

一、資料庫使用

1.使用mongodb服務,必須先開啟服務,開啟服務使用 mongod --dbpath d:mongdb    (d:mongdb  自己所建立資料庫的路徑, 在cmd視窗中輸入)   

2.管理mongodb資料庫,mongo (必須新建乙個新的cmd視窗輸入,之前開啟的cmd視窗不能關閉)

**  cls  清屏命令

二、建立資料庫

1.使用資料庫、建立資料庫

use student    如果真的想把這個資料庫建立成功,那麼必須插入乙個資料  資料庫中不能直接插入資料,只能往集合(collections)中插入資料,不需要專門建立集合,只需要寫點語法插入資料就會建立集合

db.student.insert(); //插入資料

show collections  就能看到剛才建立的集合(student)

2.刪除當前所在的資料庫

db.dropdatabase();

刪除集合語法  db.collection_name.drop

db.student.drop()   

三、插入資料

db.student.insert(); //插入資料

四、查詢資料

1.查詢所有記錄

db.student.find()    

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

db.student.disnct("name")              //會過濾掉相同的資料,只顯示一條

3.查詢age="25"的記錄

db.sutdent.find()     //只查詢出一條資料

4.查詢age>22的記錄

db.student.find(})

5.查詢age<22的記錄

db.student.find(})

6.查詢age>=25的記錄

db.student.find(})

7.查詢age<=25的記錄

db.student.find(})

8.查詢age>=23 並且age<=26

db.student.find(})

9.查詢name中包含moongo的資料    模糊查詢用於搜尋

db.student.find()

10.查詢name中以mongo開頭的

db.student.find()

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

db.sutdent.find({},)

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

db.student.fiind(},)

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

db.student.find().sort()    按年齡公升序排序

db.studnet.find().sort()   按年齡降序排序

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

db.student.find()

15.查詢前5條資料

db.student.find().limit(5)

16.查詢10條以後的資料

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

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

db.student.find().limit(10).skip(5);   //可用於分頁 ,limit是pagesize,skip是第幾頁 *(乘以)pagesize

18. or與查詢

db.student.find(,]})   查詢age22或者25的資料

19.查詢第一條資料  findone

db.student.findone()

20.查詢某個結果集的記錄條數

db.student.find(}).count()    查詢age大於25的資料

如果要返回限制之後的記錄數量,要使用 count(true)或者 count(非 0) db.users.find().skip(10).limit(5).count(true);

四、修改資料

1.db.student.update(,});              查詢名字叫做小明的,把年齡更改為 16 歲:

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

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

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

5.db.users.update(, }, false, true); 相當於:update users set age = age + 50 where name = 『lisi』;

6.db.users.update(, , $set: }, false, true); 相當於:update users set age = age + 50, name = 『hoho』 where name = 『lisi』;

五、刪除資料

db.collectionsnames.remove( )     刪除集合

db.users.remove();

mongdb資料庫基本操作

插入一條db物件 public static void adddbobject dbcollection collection,basicdbobject object 根據id查詢dbobject public static dbobject getdbobjectbyid string valu...

MongoDB資料庫《1》 MongDB資料庫基礎

mongodb埠號 27017 1.基本操作mongo linux下開啟mongodb mongodb dbpath 存放資料的位址 exit 退出mongodb2.庫級操作use 資料庫名 有則進入,無則建立 db 檢視當前所在的資料庫 show dbs 檢視所有資料庫,資料庫中沒有資料的不會顯示...

python與mongdb資料庫的連線

python與mongdb資料庫的連線 from pymongo import mongoclient 1.使用客戶端連線伺服器 conn mongoclient 127.0.0.1 27017 2.選擇要操作的資料庫 db conn.mydb1 3.選擇資料庫中要操作的集合 collection ...